简体   繁体   English

我需要使用Java中的DOM将元素添加到xml文件中

[英]I need to add an element to an xml file using the DOM in Java

I've a problem adding elements to my xml file using the DOM in my Java 我在Java中使用DOM将元素添加到xml文件中时遇到问题

I know there is a lot of solutions to similar questions, but I haven't been successful in getting them to work for me, any help would be appreciated. 我知道有很多解决类似问题的方法,但是我没有成功地使它们为我服务,任何帮助将不胜感激。 thanks guys and girls! 谢谢男孩和女孩!

My current method for adding a new element 我当前添加新元素的方法

public static void createEntryXmlFile () {
  try{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(FILE)); 


        NodeList nodes = document.getElementsByTagName("CATALOG");


        // cd elements
        Element cd = document.createElement("CD");


        // title elements
        Element title = document.createElement("TITLE");
        title.appendChild(document.createTextNode("TITLE")); 
        cd.appendChild(title);

        // artist elements
        Element artist = document.createElement("ARTIST");
        artist.appendChild(document.createTextNode("ARTIST")); 
        cd.appendChild(artist);

        // company elements
        Element company  = document.createElement("COMPANY");
        company.appendChild(document.createTextNode("COMPANY")); 
        cd.appendChild(company);

        // country elements
        Element country = document.createElement("COUNTRY");
        country.appendChild(document.createTextNode("COUNTRY")); 
        cd.appendChild(country);

        // price elements
        Element price = document.createElement("PRICE");
        price.appendChild(document.createTextNode("PRICE")); 
        cd.appendChild(price);

        // year elements
        Element year = document.createElement("YEAR");
        year.appendChild(document.createTextNode("YEAR")); 
        cd.appendChild(year);

        Node n = (Element) cd;
        nodes.item(0).appendChild(cd);


  } catch (ParserConfigurationException | SAXException | IOException e) {
        System.out.println(e);
    }
}

Copy of the XML file XML文件的副本

<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
</CATALOG>

The code you've shown will parse an existing XML file into a DOM tree in memory and then make changes to that tree, but modifying the tree in memory won't affect the file on disk, you need to explicitly write it back out yourself. 您显示的代码将现有的XML文件解析为内存中的DOM树,然后对该树进行更改,但是修改内存中的树不会影响磁盘上的文件,您需要自己明确地将其写回。

The two most common ways to do that are a no-op Transformer from a DOMSource to a StreamResult, or by using the DOM "load and store" API by casting document.getImplementation() to a DOMImplementationLS . 两种最常见的实现方法是从DOMSource到StreamResult的无操作Transformer ,或者通过将document.getImplementation()转换为DOMImplementationLS来使用DOM“加载和存储” API。 Since the Transformer approach uses the XSLT infrastructure it may not play nicely with a non-namespace-aware DOM tree such as you're using ( DocumentBuilderFactory is not namespace aware by default, you have to explicitly configure it so before you call newDocumentBuilder ) but the LS approach will definitely be OK as that's entirely part of the DOM library, it doesn't involve the transformation layer. 由于Transformer方法使用XSLT基础结构,因此可能无法与您正在使用的非名称空间感知DOM树配合使用(默认情况下, DocumentBuilderFactory不支持名称空间,因此必须显式配置它,以便在调用newDocumentBuilder之前) LS方法肯定是可以的,因为它完全是DOM库的一部分,它不涉及转换层。

Add the following code after your DOM manipulation statements so that changes are written into the FILE: 在DOM操作语句之后添加以下代码,以便将更改写入FILE:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(document), new StreamResult(new File(FILE)));
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
} catch(TransformerException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM