Generate/get xpath from XML node java

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

    With jOOX (a jquery API port to Java, disclaimer - I work for the company behind the library), you can almost achieve what you want in a single statement:

    // I'm assuming this:
    import static org.joox.JOOX.$;
    
    // And then...
    List coolList = $(document).xpath("//*[not(*)]").map(
        context -> $(context).xpath() + "='" + $(context).text() + "'"
    );
    

    If document is your sample document:

    
        one
        two
        three
        four
        
            five
        
    
    

    This will produce

    /root[1]/elemA[1]='one'
    /root[1]/elemA[2]='two'
    /root[1]/elemB[1]='three'
    /root[1]/elemA[3]='four'
    /root[1]/elemC[1]/elemB[1]='five'
    

    By "almost", I mean that jOOX does not (yet) support matching/mapping attributes. Hence, your attributes will not produce any output. This will be implemented in the near future, though.

提交回复
热议问题