0

I have an XML-String and, I want to get all result-elements with their messageId-,designation- and status-members from it. How can I do that?

"null
<?xml version="1.0" encoding="UTF-8"?>
<results>
    <result>
        <status>-13</status>
        <messageid></messageid>
        <destination>null</destination>
    </result>
    <result>
        <status>-3</status>
        <messageid></messageid>
        <destination>911234567898</destination>
    </result>
    <result>
        <status>0</status>
        <messageid>146092209473920945</messageid>
        <destination>917827767338</destination>
    </result>
    <result>
        <status>0</status>
        <messageid>116092209473924510</messageid>
        <destination>918527593928</destination>
    </result>
    <result>
        <status>-3</status>
        <messageid></messageid>
        <destination>912345234567</destination>
    </result>
</results>"

Please help me to solve the problem.

midor
  • 5,487
  • 2
  • 23
  • 52
Betty
  • 31
  • 2
  • 4
  • 1
    Is the `null` at the start really there? If so, you should work out why - that's not part of a valid XML document. Next, use an XML parser to parse the XML - there are *lots and lots* of XML tutorials around on the web. If you have problems, ask a specific question. – Jon Skeet Sep 22 '16 at 11:04

2 Answers2

0

First of all you need to get rid of the null part at the start of the String. A InputStream can be created from the rest of the String (see How do I turn a String into a InputStreamReader in java?). Parse that stream with a DOM parser and extract the messageids with xpath:

String s = "null<?xml version=\"1.0\" encoding=\"UTF-8\"?><results><result><status>-13</status><messageid></messageid><destination>null</destination></result><result><status>-3</status><messageid></messageid><destination>911234567898</destination></result><result><status>0</status><messageid>146092209473920945</messageid><destination>917827767338</destination></result><result><status>0</status><messageid>116092209473924510</messageid><destination>918527593928</destination></result><result><status>-3</status><messageid></messageid><destination>912345234567</destination></result></results>";
InputStream is = new ByteArrayInputStream(s.substring(s.indexOf("<?xml")).getBytes(StandardCharsets.UTF_8));

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document document = builder.parse(is);
XPath path = XPathFactory.newInstance().newXPath();

String expression = "//messageid";

NodeList list = (NodeList) path.evaluate(expression, document, XPathConstants.NODESET);
List<String> messageIds = new ArrayList<>(list.getLength());
for (int i = 0, l = list.getLength(); i < l; i++) {
    messageIds.add(list.item(i).getTextContent());
}
System.out.println(messageIds);
Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
0
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource src = new InputSource();
src.setCharacterStream(new StringReader(xml));//your string xml data

Document doc = builder.parse(src);
String status= doc.getElementsByTagName("status").item(0).getTextContent();
String messageid=doc.getElementsByTagName("messageid").item(0).getTextContent();
String designation=doc.getElementsByTagName("designation").item(0).getTextContent();