用于XML编辑,更新的DOM解析器

时间:2018-09-04 06:11:41

标签: android xml parsing dom xml-parsing

对于所有帮助我使用DOM解析器解析XML的帖子,我表示感谢。

我不知道如何使用DOM解析器在XML文件中动态添加如下数据,任何人都可以帮助我摆脱困境。

谢谢。

<group
    android:name="Marker Group Left Front Wing Mirror Cover"
    android:scaleX="1"
    android:scaleY="1"
    android:translateX="1778.866765"
    android:translateY="331.3570720">
    <path
        android:name="Marker Red"
        android:fillColor="#ff0031"
        android:pathData="M36.387 0.355c-19.829 0 -35.959 16.13 -35.959 35.959 0 24.607 32.177 60.732 33.546 62.26 1.288 1.429 3.535 1.427 4.821 0 1.369 -1.526 33.551 -37.65 33.551 -62.26 0 -19.829 -16.13 -35.959 -35.959 -35.959zm0 54.053c-9.976 0 -18.093 -8.116 -18.093 -18.094 0 -9.976 8.117 -18.091 18.093 -18.091 9.978 0 18.095 8.115 18.095 18.091 0 9.978 -8.117 18.094 -18.095 18.094z" />
</group>

我想将以上数据添加到以下所述的节点:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="1800"
android:viewportWidth="2880">

NEED TO ADD ABOVE group node HERE

</vector>

1 个答案:

答案 0 :(得分:0)

在元素和属性上进行了几次研发之后,我终于找到了解决方案。

Element group = doc.createElement("group");
    group.setAttribute("android:name", "Marker Group Left Front Wing Mirror Cover");
    group.setAttribute("android:scaleX", "1");
    group.setAttribute("android:scaleY", "1");
    group.setAttribute("android:translateX", "1778.866765");
    group.setAttribute("android:translateY", "331.3570720");

    Element root = doc.getDocumentElement();
    (here root is my vector in which i want to add group element)
    root.appendChild(group);
  • 上面的代码会将群组添加到我的引导程序中。

    Element path = doc.createElement("path");
    path.setAttribute("android:name", "Marker Red");
    path.setAttribute("android:fillColor", "#ff0031");
    path.setAttribute("android:pathData", "M36.387 0.355c-19.829 0 -35.959 16.13 -35.959 35.959 0 24.607 32.177 60.732 33.546 62.26 1.288 1.429 3.535 1.427 4.821 0 1.369 -1.526 33.551 -37.65 33.551 -62.26 0 -19.829 -16.13 -35.959 -35.959 -35.959zm0 54.053c-9.976 0 -18.093 -8.116 -18.093 -18.094 0 -9.976 8.117 -18.091 18.093 -18.091 9.978 0 18.095 8.115 18.095 18.091 0 9.978 -8.117 18.094 -18.095 18.094z");
    
    
    root.getLastChild().appendChild(path);
    
  • 以上代码会将路径添加到我的组,这是根元素向量的最后一个子项。
相关问题