XPATH Examples - Oracle Parser

28 Nov 1999 17:00

 

 

Traversing the DOM using XPATH is much easier than writing all of the node traversal logic yourself - there is less code and therefore fewer bugs.  On the other hand, the DOM is faster than XPATH so only use it when it will replace a lot of code.

See the selectNodes() method on XMLNode


Example I

Here is an email from an Oracle engineer:

Using the method requires providing a pattern and a NSResolver (namespace resolver). I'm not making heavy use of namespaces, so I believe you can pass the element itself as the namespace resolver.

Attached is a set of helper methods I use on top of selectNodes() which eventually should get pushed down into the V2 parser implementation for making this feature even easier to use:

With my helper methods, I can just do:

   NodeList nl = XSLT.select("this/that[@theother='foo']");

or

   System.out.println("This is the value of "
    +XSLT.valueOf("/something//like[@this='5']"));


Quite powerful when you use the XPath expression document('foo.xml') in the expression to selectNodes()...


Example II

public static final String ACTION_DELETE = "*/*[@rep_action=" + "\'" + "delete" + "\']";

public static void processMessage(String xmlRequestDoc)
{
   boolean success = false;

   // Parse the xmlRequestDoc string to a DOM
   DOMParser parser = new DOMParser();
   byte [] aByteArr = xmlRequestDoc.getBytes();
   ByteArrayInputStream bais = new ByteArrayInputStream(aByteArr , 0, aByteArr.length);
   parser.parse(bais);
   XMLDocument xmlDoc = (XMLDocument) parser.getDocument();

   org.w3c.dom.NodeList nl = xmlDoc.selectNodes(ACTION_DELETE, null);

   for (int i = 0; i < nl.getLength(); i++)
   {
      org.w3c.dom.Node node = nl.item(i);
      String elementType = node.getNodeName();
   }
}