How to read XML using XPath in Java

后端 未结 8 1392
无人及你
无人及你 2020-11-21 05:38

I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement.

here is what I want to do

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 06:21

    Here is an example of processing xpath with vtd-xml... for heavy duty XML processing it is second to none. here is the a recent paper on this subject Processing XML with Java – A Performance Benchmark

    import com.ximpleware.*;
    
    public class changeAttrVal {
        public  static  void main(String s[]) throws VTDException,java.io.UnsupportedEncodingException,java.io.IOException{
            VTDGen vg = new VTDGen();
            if (!vg.parseFile("input.xml", false))
                return;
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            XMLModifier xm = new XMLModifier(vn);
            ap.selectXPath("/*/place[@id=\"p14\" and   @initialMarking=\"2\"]/@initialMarking");
            int i=0;
            while((i=ap.evalXPath())!=-1){
                xm.updateToken(i+1, "499");// change initial marking from 2 to 499
            }
            xm.output("new.xml");
        }
    
    }
    

提交回复
热议问题