【问题标题】:XML Parsing from Java Rest Service Response从 Java Rest 服务响应解析 XML
【发布时间】:2015-07-06 13:16:25
【问题描述】:

我正在从 Java Rest Service 获得以下 XML 响应。有人知道如何获取 status 标记信息吗?

<operation name="EDIT_REQUEST">
<result>
<status>Failed</status>
<message>Error when editing request details - Request Closing Rule Violation. Request cannot be Resolved</message>
</result>
</operation>

【问题讨论】:

标签: java xml jakarta-ee xml-parsing


【解决方案1】:

XPath 是一种非常健壮且直观的快速查询 XML 文档的方法。您可以通过以下步骤达到status标签的值。

   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
   documentBuilderFactory.setNamespaceAware(true);
   DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
   Document doc = builder.parse(stream); //input stream of response.

   XPathFactory xPathFactory = XPathFactory.newInstance();
   XPath xpath = xPathFactory.newXPath();

   XPathExpression expr = xpath.compile("//status"); // Look for status tag value.
   String status =  expr.evaluate(doc);
   System.out.println(status);

【讨论】:

    【解决方案2】:

    这里有一个粗略的代码

        ByteArrayInputStream input =  new ByteArrayInputStream(
                response.getBytes("UTF-8"));
    
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(input);
    
        // first get root tag and than result tag and childs of result tag
        NodeList childNodes = doc.getDocumentElement().getChildNodes().item(0).getChildNodes();
        for(int i = 0 ; i < childNodes.getLength(); i++){
            if("status".equals(childNodes.item(i).getNodeName())){
                System.out.println(childNodes.item(i).getTextContent());
            }
        } 
    

    【讨论】:

      【解决方案3】:

      如果您只关心状态文本,那么使用一个简单的正则表达式与组怎么样?

      例如

      String responseXml = 'xml response from service....'
      
      Pattern p = Pattern.compile("<status>(.+?)</status>");
      Matcher m = p.matcher(responseXml);
      
      if(m.find())
      {
         System.out.println(m.group(1)); //Should print 'Failed'
      }
      

      【讨论】:

        【解决方案4】:

        一种选择是使用 库,使用 导入,例如:

        compile 'org.jooq:joox:1.3.0'
        

        并像这样使用它:

        import org.xml.sax.SAXException;
        import java.io.File;
        import java.io.IOException;
        import static org.joox.JOOX.$;
        
        public class Main {
        
            public static void main(String[] args) throws IOException, SAXException {
                System.out.println(
                        $(new File(args[0])).xpath("/operation/result/status").text()
                );
            }
        }
        

        它产生:

        Failed
        

        【讨论】:

        • 这不是一个 gradle 问题,也没有理由增加第三方库的负担来做一些可以用常规 Java SE 轻松完成的事情。
        猜你喜欢
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 2013-01-03
        • 2019-10-17
        • 2017-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多