0

Following is a string containing an XML file that specifies folder structure

     String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<folder name=\"c\">" +
                        "<folder name=\"program files\">" +
                            "<folder name=\"uninstall information\" />" +
                        "</folder>" +
                        "<folder name=\"users\" />" +
                    "</folder>";

If user enter initial letter of the folder the method must return the folder name (following is part of it method i have tried)

DocumentBuilder builder = 
    DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource src = new InputSource();
            src.setCharacterStream(new StringReader(xml));
            Document doc = builder.parse(src);          
            List<String> listObj = new ArrayList<String>();
            if(doc.getElementsByTagName("folder").item(0) != null){
    System.out.println(doc.getElementsByTagName("folder").item(0).getNodeValue());}

But in my method i always get null. How do I iterate and find out the folder name?

1 Answer 1

1

name is an Attribute of the Folder tag, so you have to use the following code:

System.out.println(doc.getElementsByTagName("folder").item(0).getAttributes().getNamedItem("name").getNodeValue());
Sign up to request clarification or add additional context in comments.

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.