【问题标题】:DOM parser for XML Edit, Update用于 XML 编辑、更新的 DOM 解析器
【发布时间】:2019-02-09 02:31:50
【问题描述】:

感谢所有帮助我使用 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】:

    在元素和属性上做了几次研发,终于找到了解决办法。

    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);
      
    • 上面的代码会将路径添加到我的组,它是根元素向量的最后一个子元素。

    【讨论】:

      猜你喜欢
      • 2011-11-24
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2013-07-24
      • 2010-11-24
      相关资源
      最近更新 更多