Simplest way to query XML in Java

后端 未结 9 489
南旧
南旧 2020-11-30 05:55

I have small Strings with XML, like:

String myxml = \"goodhi\";
<
相关标签:
9条回答
  • 2020-11-30 06:34

    You can use Jerry to query XML similar to jQuery.

    jerry(myxml).$("status")
    
    0 讨论(0)
  • 2020-11-30 06:36

    Here is a code snippet of querying your XML with VTD-XML

    import com.ximpleware.*;
    public class simpleQuery {
    
        public static void main(String[] s) throws Exception{
            String myXML="<resp><status>good</status><msg>hi</msg></resp>";
            VTDGen vg = new VTDGen();
            vg.setDoc(myXML.getBytes());
            vg.parse(false);
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            ap.selectXPath("/resp/status");
            int i = ap.evalXPath();
            if (i!=-1)
                System.out.println(" result ==>"+vn.toString(i));
        }
    }
    
    0 讨论(0)
  • 2020-11-30 06:43

    convert this string into a DOM object and visit the nodes:

    Document dom= DocumentBuilderFactory().newDocumentBuilder().parse(new InputSource(new StringReader(myxml)));
    Element root= dom.getDocumentElement();
    for(Node n=root.getFirstChild();n!=null;n=n.getNextSibling())
     {
     System.err.prinlnt("Current node is:"+n);
     }
    
    0 讨论(0)
提交回复
热议问题