用于XML Edit,Update的DOM解析器

问题描述 投票:0回答:1

我很感谢能帮助我使用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>
android xml parsing dom xml-parsing
1个回答
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);
  • 上面的代码将添加我的组的路径,这是根元素向量的最后一个子。
© www.soinside.com 2019 - 2024. All rights reserved.