0

Here is the example XML and let's say I want to extract the data "DEF". Any idea how to extract by combining node and attribute value i.e. Node should be "Container" and attribute value should be "2" ? I am new to JDOM and XML parsing. Please give your suggestions.

<?xml version="1.0"?>
<Product>
    <Property>
        <Container value="1">ABC</Container>
        <Container value="2">DEF</Container>
        <Container value="3">GHI</Container>
    </Property>
</Product>
0

1 Answer 1

1

like this

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.Attributes;

public class ReadXMLFile {
    public static void main(String[] args) {

        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("src/jdom.xml");

        try {

            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            Element element = rootNode.getChild("Property");
            List list = element.getChildren("Container");

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

                Element node = (Element) list.get(i);

                Attribute value = node.getAttribute("value");
                if ((value.getValue().equals("2"))) {
                    System.out.println(value.getValue());

                    System.out.println("values : " + node.getText());
                }

            }

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is a great example for JDOM 1.x For JDOM 2.x you may consider the for loop to be better as for (Element node : element.getChildren("Container")) { ..... Got my +1. Thanks
@rolfl Yeah for loop works that way too :) Just wondering is JDOM better than other parsers ? or what is the advantage of JDOM ?
@SumitSahoo - I am biased, I'm the maintainer of JDOM. Also, JDOM is not a parser, it just makes using a parser really, really easy. ;-)
@rolfl ... Yeah I read the other day that this uses the standard Java parser but it handles memory a bit efficiently. Am I correct ?
That is a fair summary. It is best to compare it with DOM (which JDOM replaces). JDOM is designed for Java (unlike DOM) and allows all the conventional Java functionality to work - Generics, Collections, Enums, etc. It just works the way a Java person would expect. Read up on JDOM Getting Started
|

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.