4

I am currently parsing XML, but im not quite sure how to parse the "status" attribute of "message":

<message status="test"> <text>sometext</text> <msisdn>stuff</msisdn> </message>

Here is the code, i have cut off everything unnecessary:

NodeList nodeLst = doc.getElementsByTagName("message");

for (int s = 0; s < nodeLst.getLength(); s++) {

       Node fstNode = nodeLst.item(s);

       if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

               Element fstElmnt = (Element) fstNode;

               NodeList numberNmElmntLst = fstElmnt
               .getElementsByTagName("msisdn");
               Element numberNmElmnt = (Element) numberNmElmntLst.item(0);
               NodeList numberNm = numberNmElmnt.getChildNodes();
               String phoneNumber = ((Node) numberNm.item(0))
               .getNodeValue().substring(2);

               NodeList txtNmElmntLst = fstElmnt
               .getElementsByTagName("text");
               Element txtNmElmnt = (Element) txtNmElmntLst.item(0);
               NodeList txtNm = txtNmElmnt.getChildNodes();
               String text = ((Node) txtNm.item(0)).getNodeValue();

               NodeList rcvNmElmntLst = fstElmnt
               .getElementsByTagName("received");
               Element rcvNmElmnt = (Element) rcvNmElmntLst.item(0);
               NodeList rcvNm = rcvNmElmnt.getChildNodes();
               String recievedDate = ((Node) rcvNm.item(0)).getNodeValue();
            }
}       

Can anyone guide me how this is done?

Thanks in advance.

2 Answers 2

12

Node.getAttributes()

NamedNodeMap attributes = fstElmnt.getAttributes();

for (int a = 0; a < attributes.getLength(); a++) 
{
        Node theAttribute = attributes.item(a);
        System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
}

You could avoid traversing if you use XPATH to retrieve the data. Read this tutorial.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your reply, can you explain the attribute aNode?
@JavaCake aNode refers to the node for which you want to retrieve the attribute. In your case it is the Node referring to "message" element.
do you mind giving implementation example based on my code? I cant exactly determine how its done.
@JavaCake i edited my code. I assume "message" is the root element of your xml. if it is then it will work. try it and let me know. You can get the root element using doc.getDocumentElement() in DOM
@JavaCake i just realized "message" is not root element. you need to call that method on fstElmnt. i edited my code
|
2

I have been playing with Apache Xerces for parsing DOM. But it was horrible tasks. If you could, take a look at jsoup.

So, if your question has an answer in Jsoup, it would be:

node.attr("status")

1 Comment

I regret i did not do that, but unfortunately i cannot change much now, the application is very big.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.