如何使用DOM解析器检索包括标签的XML

时间:2013-06-06 21:16:28

标签: java xml dom xml-parsing

我使用org.w3c.dom来解析XML文件。然后我需要返回包含标签的特定节点的整个XML,而不仅仅是标签的值。我正在使用NodeList,因为我需要计算文件中有多少条记录。但我还需要从头开始阅读批处理文件,然后将其写入新的XML文件。但我当前的代码只打印节点的值,而不是节点本身。我很难过。

public static void main(String[] args) {
    try {
        File fXmlFile = new File (args[0]);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList listOfRecords = doc.getElementsByTagName("record");

        int totalRecords = listOfRecords.getLength();
        System.out.println("Total number of records : " + totalRecords);
        int amountToSplice = queryUser();

        for (int i = 0; i < amountToSplice; i++) {
            String stringNode = listOfRecords.item(i).getTextContent();
            System.out.println(stringNode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:2)

getTextContent()只会“返回此节点及其后代的文本内容”,即您只获取“text”类型节点的内容。解析XML时,最好记住有几种不同类型的节点,请参阅XML DOM Node Types

要做你想做的事,你可以创建一个像这样的实用工具......

public static String nodeToString(Node node) 
{
  Transformer t = TransformerFactory.newInstance().newTransformer();
  t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  t.setOutputProperty(OutputKeys.INDENT, "yes");
  StringWriter sw = new StringWriter();
  t.transform(new DOMSource(node), new StreamResult(sw));
  return sw.toString();
}

然后像这样循环和打印......

for (int i = 0; i < amountToSplice; i++)
  System.out.println(nodeToString(listOfRecords.item(i)));