Alex Rivera | Logout

Generate/get Xpath from XML in Java

Asked 2011-01-20T11:02:49.720
42

I'm interested in advice/pseudocode code/explanation rather than actual implementation.

  • I'd like to go through XML document, all of its nodes
  • Check the node for attribute existence

Case if node doesn't have attribute, get/generate String with value of its xpath
Case if node does have attributes, iterate through attribute list and create xpath for each attribute including the node as well.

Edit

My reason for doing this is: I'm writing automated tests in Jmeter, so for every request I need to verify that request actually did its job so I'm asserting results by getting nodes values with Xpath.

When the request is small it's not a problem to create asserts by hand, but for larger ones it's really a pain.

I'm looking for Java approach.

Goal

My goal is to achieve following from this example XML file :

<root>
    <elemA>one</elemA>
    <elemA attribute1='first' attribute2='second'>two</elemA>
    <elemB>three</elemB>
    <elemA>four</elemA>
    <elemC>
        <elemB>five</elemB>
    </elemC>
</root>

to produce the following :

//root[1]/elemA[1]='one'
//root[1]/elemA[2]='two'
//root[1]/elemA[2][@attribute1='first']
//root[1]/elemA[2][@attribute2='second']
//root[1]/elemB[1]='three'
//root[1]/elemA[3]='four'
//root[1]/elemC[1]/elemB[1]='five'

Explained :

  • If node value/text is not null/zero, get xpath , add = 'nodevalue' for assertion purpose
  • If node has attributes create assert for them too

Update

I found this example, it doesn't produce the correct results, but I'm looking something like this:

http://www.coderanch.com/how-to/java/SAXCreateXPath

Edit
Report

2 Answers

2
  1. use w3c.dom
  2. go recursively down
  3. for each node there is easy way to get it's xpath: either by storing it as array/list while #2, or via function which goes recursively up until parent is null, then reverses array/list of encountered nodes.

something like that.

UPD: and concatenate final list in order to get final xpath. don't think attributes will be a problem.

answered 2011-01-20T11:55:31.213
1

I did the exact same thing last week for processing my xml to solr compliant format.

Since you wanted a pseudo code: This is how I accomplished that.

// You can skip the reference to parent and child.

1_ Initialize a custom node object: NodeObjectVO {String nodeName, String path, List attr, NodeObjectVO parent, List child}

2_ Create an empty list

3_ Create a dom representation of xml and iterate thro the node. For each node, get the corresponding information. All the information like Node name,attribute names and value should be readily available from dom object. ( You need to check the dom NodeType, code should ignore processing instruction and plain text nodes.)

// Code Bloat warning. 4_ The only tricky part is get path. I created an iterative utility method to get the xpath string from NodeElement. (While(node.Parent != null ) { path+=node.parent.nodeName}.

(You can also achieve this by maintaining a global path variable, that keeps track of the parent path for each iteration.)

5_ In the setter method of setAttributes (List), I will append the object's path with all the available attributes. (one path with all available attributes. Not a list of path with each possible combination of attributes. You might want to do someother way. )

6_ Add the NodeObjectVO to the list.

7_ Now we have a flat (not hierrarchial) list of custom Node Objects, that have all the information I need.

(Note: Like I mentioned, I maintain parent child relationship, you should probably skip that part. There is a possibility of code bloating, especially while getparentpath. For small xml this was not a problem, but this is a concern for large xml).

answered 2011-01-23T15:50:32.767

Your Answer