Generate/get xpath from XML node java

前端 未结 8 701
清酒与你
清酒与你 2020-11-22 10:39

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

  • I\'d like to go trough xml document
相关标签:
8条回答
  • 2020-11-22 11:26
    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.

    0 讨论(0)
  • 2020-11-22 11:28

    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).

    0 讨论(0)
提交回复
热议问题