无法使用Java从XML读取节点元素及其值

问题描述 投票:0回答:1

我无法使用XPath和DOMParser读取XML元素及其值。客户端在我的应用程序中将此XML作为请求发送,我无法控制操作客户端代码。我想使用DOMParser读取AccountID。

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns="https://billpayment.weaut.com/">
  <soap:Body>
    <GetAccountBalanceByAccount xmlns="https://billpayment.weaut.com/">
      <CompanyName>AABC</CompanyName>
      <Language>ENG</Language>
      <AccountID>54698214</AccountID>
    </GetAccountBalanceByAccount>
  </soap:Body>
</soap:Envelope>

这就是我试图解析XML以获取AccountID节点及其内容的方法。

@RequestMapping(value = "/", method = { RequestMethod.POST}, consumes = {"text/xml"}, produces = "text/xml")    
    public ResponseEntity messageStub(@RequestBody String requestString)
            throws ClientProtocolException, IOException {       
        try {           
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();          
            Document doc = db.parse(new InputSource(new StringReader(requestString)));          
            XPathFactory xPathFactory = XPathFactory.newInstance();
            XPath xpath = xPathFactory.newXPath();
            javax.xml.namespace.NamespaceContext ns = new javax.xml.namespace.NamespaceContext()
            {               
                    @Override
                    public String getNamespaceURI(String prefix) 
                    {
                        if ( "soap".equals( prefix ) )
                        {                           
                            return "http://www.w3.org/2003/05/soap-envelope";
                        }                                                                                                              
                        return javax.xml.XMLConstants.NULL_NS_URI;
                   }
                   @Override
                   public String getPrefix(String namespaceURI) 
                   {
                      return null;
                   }
                    @Override
                    public Iterator<?> getPrefixes(String namespaceURI) 
                    {
                        return null;
                    }
            };       
            xpath.setNamespaceContext(ns);
            String nodeName="", nodeContent="";         
            NodeList nodeList = null;
            //XML Path
            String chkXMLPath="/soap:Envelope/soap:Body/GetAccountBalanceByAccount/AccountID";

            XPathExpression expr = xpath.compile(chkXMLPath);      
            //evaluating each node set against the requested xml
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            nodeList = (NodeList) result;                   
            //Here I am getting 0 node
            System.out.println("Got " + nodeList.getLength() + " nodes");                               
            for (int i = 0; i < nodeList.getLength(); i++) 
            {                                               
                nodeName = nodeList.item(i).getNodeName();
                nodeContent = nodeList.item(i).getTextContent();                        
                System.out.println("\nCurrent Element :" + nodeName);
                System.out.println("\nCurrent Value :" + nodeContent);
                break;
            }                                                                   
        }
    }

当我从两个位置删除xmlns命名空间后测试此方法时,我能够读取元素及其内容。您能否建议我如何在不修改XML的情况下阅读AccountID及其内容。

java eclipse soap xml-parsing
1个回答
0
投票

虽然我已经通过从XML中删除xmlns名称空间来解决了这个问题。我使用以下方法来执行此操作。这为我提供了适当的XML。

public static String RemoveAllXmlNamespace(String xmlData)
     {
     //Regex for xmlNS
         String xmlnsRegex = "\\s+xmlns\\s*(:\\w)?\\s*=\\s*\\\"(?<url>[^\\\"]*)\\\"";
         Pattern p = Pattern.compile(xmlnsRegex);
         Matcher m = p.matcher(xmlData); //get a matcher object
         int count = 0;
         while(m.find())
         {                 
           String replaceString= xmlData.substring(m.start(), m.end());
           //Removing xmlNS from the XML String
           xmlData = xmlData.replace(replaceString, "");
           System.out.println("xmlData: "+xmlData);
           break;
         }
         return xmlData;
    }

上面的代码是我认为摆脱不需要的命名空间最安全的方法。此方法在输出中提供以下XML。

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <GetAccountBalanceByAccount>
      <CompanyName>AABC</CompanyName>
      <Language>ENG</Language>
      <AccountID>54698214</AccountID>
    </GetAccountBalanceByAccount>
  </soap:Body>
</soap:Envelope>
© www.soinside.com 2019 - 2024. All rights reserved.