0

I have a problem while running the following code

SAXBuilder builder = new SAXBuilder();
    File xmlFile = new File("C:\\Users\\V\\Documents\\douleia\\temp\\dokimi.xml");

    try {

        Document document = (Document) builder.build(xmlFile);
        Element rootNode = document.getRootElement();
        List list = rootNode.getChildren("safety_application");



        for (int i = 0; i < list.size(); i++) {

               Element element = (Element) list.get(i);
               System.out.println("number :" + element.getAttributeValue("id"));
               System.out.println("element : " + element.getChildText("element"));
               System.out.println("File_name : " + element.getChildText("name"));  

            }


        }


    catch (Exception e) {
        e.printStackTrace();
        }

I don't get any errors but it doesn't print anything. Meaning that the program is supposed to print number: 1 element: element File_name: AND I get an output like this []

My XML file has this form

<safety_application>
<element id="1">
    <name>AND</name>
</element>
<element id="2">
    <name>OR</name>
</element>
</safety_application>

1 Answer 1

1

Your root element is "safety_application". try this:

List list = rootNode.getChildren("element");
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.