Generate/get xpath from XML node java

前端 未结 8 764
清酒与你
清酒与你 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:11

    I've done a similar task once. The main idea used was that you can use indexes of the element in the xpath. For example in the following xml

    
        
        
        
    
    

    xpath to the second will be /root[1]/el[2] (xpath indexes are 1-based). This reads as "take the first root, then take the second one from all elements with the name el". So element something does not affect indexing of elements el. So you can in theory create an xpath for each specific element in your xml. In practice I've accomplished this by walking the tree recursevely and remembering information about elements and their indexes along the way.
    Creating xpath referencing specific attribute of the element then was just adding '/@attrName' to element's xpath.

提交回复
热议问题