1

我是 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>

请建议我我们该怎么做。提前致谢

4

1 回答 1

2

这是您可以执行的操作的示例:

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>

更新:添加了插入孙子的代码。

于 2012-05-28T18:09:34.917 回答