Sean
Sean

Reputation: 253

Efficient method to find nodes by tag and attribute

I have to update a strictly defined (e.g. can't alter the format) XML document. I am using the DOM parser to load the file and update where appropriately. Unfortunately, the document does supply ids to anything, so I am forced to use getElementsByTagName to find the nodes/elements I need to update.

I haven't had any issues yet, but have just come across a section of text like:

<types>
  <type type_def_id="1" type_value="008" />
  <type type_def_id="6" type_value="uhl" />
  <type type_def_id="9" type_value="xpm" />
  <type type_def_id="11" type_value="4100" />
</types>

Using getElementsByTagName, I would need to iterate through the NodeList finding the type_def_id I need to updated which doesn't seem to be the best approach.

Any suggestions using Java 1.4?

Upvotes: 1

Views: 271

Answers (1)

LarsH
LarsH

Reputation: 27994

As @biziclop suggested, XPath would be an efficient method in terms of programmer time (and also probably in terms of CPU time).

Here is a primer on using the javax.xml.xpath package in Java 5.

A code sample based on the above article would be:

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("myInput.xml");

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//type[type_def_id = '9']");

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
    // do what you need to do...
}

Upvotes: 3

Related Questions