alig227
alig227

Reputation: 357

How to retrieve XML including tags using the DOM parser

I am using org.w3c.dom to parse an XML file. Then I need to return the ENTIRE XML for a specific node including the tags, not just the values of the tags. I'm using the NodeList because I need to count how many records are in the file. But I also need to read the file wholesale from the beginning and then write it out to a new XML file. But my current code only prints the value of the node, but not the node itself. I'm stumped.

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();
    }
}

Upvotes: 2

Views: 1070

Answers (1)

davmos
davmos

Reputation: 9577

getTextContent() will only "return the text content of this node and its descendants" i.e. you only get the content of the 'text' type nodes. When parsing XML it's good to remember there are several different types of node, see XML DOM Node Types.

To do what you want, you could create a utility method like this...

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();
}

Then loop and print like this...

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

Upvotes: 2

Related Questions