我们可以使用DOM解析器在现有xml文件中的特定位置添加元素吗?

时间:2012-05-25 17:04:44

标签: java xml-parsing

我是DOM解析的新手,他们需要使用DOM解析器在现有xml文件中的特定位置添加元素。我们将从属性文件中获取要添加的位置和元素名称,如图所示

abc.properties

3 = X
4 = Y
6 = Z

添加X,Xchild,Y和Z元素后的输出xml文件如图所示

 demo.xml  

 <root>
   <A>text</A>
   <B>text</B>
   <X>
      <Xchild>text</Xchild>
   </X>
   <Y>text</Y>
   <C>text</C>
   <Z>text</Z>
 </root>

请建议我,我们该怎么办呢。提前致谢

1 个答案:

答案 0 :(得分:1)

这是您可能会做的一个示例:

import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;

public class AddElementFromProp {

  public static void main(String[] args) 
   throws ParserConfigurationException, SAXException, IOException,
      XPathExpressionException, TransformerException, TransformerConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true); 
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("demo.xml");
    //The XPath part.
    XPathFactory xfactory = XPathFactory.newInstance();
    XPath xpath = xfactory.newXPath();

    Node result = (Node)xpath.evaluate("/root/*[position()='3']", doc, XPathConstants.NODE);
    Element toInsert = doc.createElement("X");
    result.getParentNode().insertBefore(toInsert, result);
    //////////////////////////////////////////////////////
    result = (Node)xpath.evaluate("/root/*[position()='4']", doc, XPathConstants.NODE);
    toInsert = doc.createElement("Y");
    Text txt = doc.createTextNode("text");
    toInsert.appendChild(txt);
    result.getParentNode().insertBefore(toInsert, result);
    //////////////////////////////////////////////////////
    result = (Node)xpath.evaluate("/root/*[position()='5']", doc, XPathConstants.NODE);
    toInsert = doc.createElement("Z");
    txt = doc.createTextNode("text");
    toInsert.appendChild(txt);
    result.getParentNode().appendChild(toInsert);
    ////////////////////////////////////////////////////////
    result = (Node)xpath.evaluate("/root/X", doc, XPathConstants.NODE); //If you know the name of the node
    //result = (Node)xpath.evaluate("/root/*[position()='3']", doc, XPathConstants.NODE);//If you know the position of the node
    toInsert = doc.createElement("xchild");
    txt = doc.createTextNode("text");
    toInsert.appendChild(txt);
    result.appendChild(toInsert);
    ////////////////////////////////////////////////////////
    // Write out the final xml file
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult _result = new StreamResult("demo1.xml");
    transformer.transform(source, _result);
  }
}

注意:这远不是一个强大的解决方案(仅供您参考)。这里有一堆假设。没有在任何程度上进行检查 - 例如在特定位置是否已经存在元素?它是最后一个元素的位置吗?等

输入:

<?xml version="1.0"?>
<root><A>text</A><B>text</B><C>text</C></root>

输出:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root><A>text</A><B>text</B><X><xchild>text</xchild></X><Y>text</Y><C>text</C><Z>text</Z>root>

更新:添加了代码以插入好孩子。