1

I am parsing the below XML using the Dom Parser.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company>
    <Staff id="1">
        <firstname>Achyut</firstname>
        <lastname>khanna</lastname>
        <nickname>Achyut</nickname>
        <salary>900000</salary>
    </Staff>
</company>

If I only need firstName from the XML why is returning null ?

private String getNodeValue(Node node) {
        Node nd = node.getFirstChild();     
        try {
            if (nd == null) {
                return node.getNodeValue();             
            }
            else {              
                 getNodeValue(nd);
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }
5
  • 1
    What does your debugger say? Commented Sep 2, 2013 at 9:42
  • Are you passing the Staff node as the node parameter ? Commented Sep 2, 2013 at 9:42
  • What XML library are you using? What kind of object (what package & class) is the Node argument? Commented Sep 2, 2013 at 9:44
  • Apart from the xml-libs : you must return the value of your recursive call, too. return getNodeValue(nd) for example Commented Sep 2, 2013 at 9:53
  • @Jan Piel thanks for the comment ,I forget a return statement. Thanks for reply !! Commented Sep 2, 2013 at 10:02

3 Answers 3

1

You must fetch the nodelist and then pass the appropriate Node value as parameter when you call your defined function.

NodeList n = item.getElementsByTagName("Staff");

Then call your function

String firstName = getNodeValue(n.item(0));
Sign up to request clarification or add additional context in comments.

2 Comments

but using debugger I got that the it comes to return line and show its value to Achyut but again enters inside else.Why ?
the code you wrote is a function which returns the first child of the passed Node value if it exists, here the only issue might be you are calling it with the wrong node value.
0

First of all, I do not recommend parsing XMLs using DOM traversals. I would recommend you to use the OXMs (JaxB or XMLbeans). But still if you are interested in doing it this way:

Here is the code

public class T2 {
public static void main(String []args) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilder db = null;

String xmlString = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><company>    <Staff id='1'>        <firstname>Achyut</firstname>        <lastname>khanna</lastname>        <nickname>Achyut</nickname>        <salary>900000</salary>    </Staff></company>";
Document doc = null;
InputSource is = new InputSource();

is.setCharacterStream(new StringReader(xmlString));

    db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    doc = db.parse(is);

    NodeList nodes = doc.getElementsByTagName("firstname");

    for (int i = 0; i < nodes.getLength(); i++) {
        if (nodes.item(i) instanceof Element) {
             Node node = (Node) nodes.item(i);
            nodes.item(i);

            String fName  = getCharacterDataFromElement(node);
            System.out.println(fName);
        }
    }


}
private static String getCharacterDataFromElement(Node e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return null;
}
}

The code above prints Achyut

Comments

0

Save yourself a lot of code by using XPath instead:

XPath xp = XPathFactory.newInstance().newXPath();
InputSource in = new InputSource(...);
String fn = xp.evaluate("/company/Staff[@id='1']/firstname/text()", in);
System.out.println(fn);

printts:

Achyut

Comments

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.