在Java中解析来自webservice响应的CDATA内容

时间:2021-11-01 14:05:01

I'm making a webservice call in my Java code and getting the response as String with the below data.

我正在使用我的Java代码进行webservice调用,并使用以下数据将响应作为String获取。

<DocumentElement>
   <ResultSet>
     <Response>Successfully Sent.</Response>
     <UsedCredits>1</UsedCredits>
   </ResultSet>
</DocumentElement>

Now, I want to parse the below response String and get the value of <Response> tag alone for further processing. Since, I'm getting only the CDATA content how to parse the String content?

现在,我想解析下面的响应String并单独获取 标记的值以进行进一步处理。既然如此,我只获得了CDATA内容如何解析String内容?

1 个解决方案

#1


0  

You obviously need XML parser for this one. Lets say the above is stored in a String variable named content

你显然需要这个XML解析器。可以说上面存储在一个名为content的String变量中

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
          //Using factory get an instance of document builder
           DocumentBuilder db = dbf.newDocumentBuilder();
           InputSource is = new InputSource();
           is.setCharacterStream(new StringReader(content)); //content variable holding xml records         
           Document doc = db.parse(is)
           /* Now retrieve data from xml */
           NodeList nodes = doc.getElementsByTagName("ResultSet");
           Element elm = (Element) nodes.item(0);   //will get you <Response> Successfully Sent.</Response>
           String responseText = elm.getFirstChild().getNodeValue();

    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch(SAXException se) {
        se.printStackTrace();
    }catch(IOException ioe) {
        ioe.printStackTrace();
    }

Follow the above parser routine for larger data sets. Use loops for multiple similar data sets. Hope that helps.

按照上面的解析器例程来处理更大的数据集。为多个类似的数据集使用循环。希望有所帮助。

#1


0  

You obviously need XML parser for this one. Lets say the above is stored in a String variable named content

你显然需要这个XML解析器。可以说上面存储在一个名为content的String变量中

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
          //Using factory get an instance of document builder
           DocumentBuilder db = dbf.newDocumentBuilder();
           InputSource is = new InputSource();
           is.setCharacterStream(new StringReader(content)); //content variable holding xml records         
           Document doc = db.parse(is)
           /* Now retrieve data from xml */
           NodeList nodes = doc.getElementsByTagName("ResultSet");
           Element elm = (Element) nodes.item(0);   //will get you <Response> Successfully Sent.</Response>
           String responseText = elm.getFirstChild().getNodeValue();

    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch(SAXException se) {
        se.printStackTrace();
    }catch(IOException ioe) {
        ioe.printStackTrace();
    }

Follow the above parser routine for larger data sets. Use loops for multiple similar data sets. Hope that helps.

按照上面的解析器例程来处理更大的数据集。为多个类似的数据集使用循环。希望有所帮助。