如何将xml节点添加到元素

时间:2016-11-18 10:56:52

标签: java android xml xml-parsing

下面是我当前的xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<manifest identifier="_6czyDkzacOI_course_id" version="1.0" =>

  <organizations default="Education_Loan_ORG">
    <organization identifier="Education_Loan_ORG">
      <title>Title</title>
      <item "some attrib">
        <title>ACCOUNT PLANNING TOOL</title>
      </item>
    </organization>
  </organizations>
  <resources>
    <resource identifier="__6czyDkzacOI_course_id_RES" type="webcontent" href="index.html" >
      <file href="story_content/thumbnail.jpg" />
      <file href="mobile/linen_background.jpg" />
      <file href="mobile/player.css" />
      <file href="mobile/spinner.png" />
      <file href="story_content/fonts.xml" />
      <file href="mobile/data.gz" />
      <file href="mobile/data.js" />
      <file href="mobile/masks.js" />
      <file href="story_content/blank.html" />
      <file href="launcher.html" />
      <file href="story_content/orange_launch_normal.gif" />
      <file href="story_content/story.js" />
      <file href="ioslaunch.html" />
      <file href="mobile/mobileinstall.html" />
      <file href="index.html" />
      <file href="util/NONEFunctions.js" />
      <file href="util/UtilityFunctions.js" />
    </resource>
  </resources>
</manifest>

现在我想添加另一个文件标签,如下面的

<file href="myJS.js" />

进入资源标记

这应该只使用Android来完成

下面是我正在使用的Android代码

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = null;

            documentBuilder = documentBuilderFactory.newDocumentBuilder();

            File file = new File(Environment.getExternalStorageDirectory() + "/26/imsmanifest.xml");
            Document document = documentBuilder.parse(file);

            Element root= document.getElementById("resource");
            Element name = document.createElement("file");
            name.setAttribute("href","myJS.js");

            root.appendChild(name);

请帮我实现这个目标

0 个答案:

没有答案