【问题标题】:Simplest way to query XML in Java在 Java 中查询 XML 的最简单方法
【发布时间】:2009-04-30 15:11:12
【问题描述】:

我有带有 XML 的小字符串,例如:

String myxml = "<resp><status>good</status><msg>hi</msg></resp>";

我想查询它以获取他们的内容。

最简单的方法是什么?

【问题讨论】:

    标签: java xml


    【解决方案1】:

    XPath 使用 Java 1.5 及以上版本,无外部依赖:

    String xml = "<resp><status>good</status><msg>hi</msg></resp>";
    
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    
    InputSource source = new InputSource(new StringReader(xml));
    String status = xpath.evaluate("/resp/status", source);
    
    System.out.println("satus=" + status);
    

    【讨论】:

    • 是的,我同意彼得的观点,这不是最简单的方式。这里的好处是展示了如何使用没有外部库的纯 JDK 来做到这一点。一种更简单的方法:stackoverflow.com/questions/807418/…
    • 是的,即使是使用标准 Java XML 库的简单任务也非常清楚地表明作者已经阅读了 GoF 书。但是,根据应用程序的不同,第三方库的管理可能会产生许多不同的复杂情况。
    • 这也是真的。我个人更喜欢比标准库更简单的东西,但我也对 dom4j 感到有些沮丧,这促使我写了这个问题:stackoverflow.com/questions/831865/…。 McDowell,我希望您不介意我将您的答案用作“纯 JDK”示例。 :-)
    【解决方案2】:

    使用dom4j,类似于McDowell's solution

    String myxml = "<resp><status>good</status><msg>hi</msg></resp>";
    
    Document document = new SAXReader().read(new StringReader(myxml));
    String status = document.valueOf("/resp/msg");
    
    System.out.println("status = " + status);
    

    使用 dom4j 处理 XML 稍微简单一些。以及其他几个类似的 XML 库exist。 dom4j 的替代品是discussed here

    【讨论】:

      【解决方案3】:

      这是如何使用XOM 执行此操作的示例:

      String myxml = "<resp><status>good</status><msg>hi</msg></resp>";
      
      Document document = new Builder().build(myxml, "test.xml");
      Nodes nodes = document.query("/resp/status");
      
      System.out.println(nodes.get(0).getValue());
      

      比起 dom4j,我更喜欢 XOM,因为它的 simplicity and correctness。 XOM 不会让您创建无效的 XML,即使您想这样做 ;-)(例如,在字符数据中使用非法字符)

      【讨论】:

      • 嗯,很好,这看起来确实很简单......但只是为了让比较更容易,您是否可以编辑它以完全像 McDowell 和我的解决方案一样工作? :) 我想你需要添加一三行。
      • 好的,它现在已经改变了 ;-) 你是对的......获取所有返回节点的字符串值需要一些索引。我不明白为什么没有 Nodes.getValue() 方法来返回所有返回节点的字符串值。 (我也“欺骗”了 new Builder().build(...) ;-) 但你们的代码中也有类似的链)。
      • 谢谢。你是对的链接:) 但是,这仍然很干净,很大程度上要感谢 Builder().build()。现在,如果您可以在 dom4j 中执行类似 document.valueOf(xpath) 的操作... +1
      【解决方案4】:

      你可以试试JXPath

      【讨论】:

        【解决方案5】:

        在你用简单的方法在 java 中查询 XML 之后。看看 XOM。

        【讨论】:

          【解决方案6】:

          @这个answer的cmets:

          你可以创建一个方法让它看起来更简单

          String xml = "<resp><status>good</status><msg>hi</msg></resp>";
          
          System.out.printf("satus= %s\n", getValue("/resp/status", xml ) );
          

          实现:

          public String getValue( String path, String xml ) { 
              return XPathFactory
                         .newInstance()
                         .newXPath()
                         .evaluate( path , new InputSource(
                                           new StringReader(xml)));
          
          }
          

          【讨论】:

          【解决方案7】:

          将此字符串转换为 DOM 对象并访问节点:

          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);
           }
          

          【讨论】:

            【解决方案8】:

            这是使用VTD-XML查询您的XML的代码sn-p

            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));
                }
            }
            

            【讨论】:

              【解决方案9】:

              您可以使用Jerry 来查询类似于jQuery 的XML。

              jerry(myxml).$("status")
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-07-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2010-10-06
                • 2012-02-15
                • 1970-01-01
                相关资源
                最近更新 更多