How do I navigating the XML elements tree?

Category: org.jdom, viewed: 1217 time(s).
package org.kodejava.example.jdom;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.text.MessageFormat;

public class JDOMTraversingElement {
    public static void main(String[] args) {
        String xml =
                "<root>" +
                "    <country name=\"Japan\" capital=\"Tokyo\"/>" +
                "    <country name=\"France\" capital=\"Paris\"/>" +
                "    <country name=\"Italy\" capital=\"Rome\"/>" +
                "    <country name=\"England\" capital=\"London\"/>" +
                "    <country name=\"Indonesia\" capital=\"Jakarta\"/>" +
                "    <city name=\"Denpasar\"/>" +
                "    <city name=\"Bangkok\"/>" +
                "    <city name=\"Mumbai\"/>" +
                "    <city name=\"Delhi\"/>" +
                "</root>";

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = builder.build(
                    new ByteArrayInputStream(xml.getBytes())) ;

            //
            // Getting the root element
            //
            Element root = document.getRootElement();

            //
            // Getting the first child
            //
            Element country = root.getChild("country");
            System.out.println("Name = " + country.getAttribute("name").getValue());
            System.out.println("Capital = " + country.getAttribute("capital").getValue());
            System.out.println("----------------------------------------");

            //
            // Getting all children of the root
            //
            List<Element> elements = root.getChildren();
            for (Element element : elements) {
                if (element.getName().equals("country")) {
                    System.out.println(MessageFormat.format("{0} -> {1}",
                            element.getAttribute("name").getValue(),
                            element.getAttribute("capital").getValue()));
                } else if (element.getName().equals("city")) {
                    System.out.println(element.getAttribute("name").getValue());
                }
            }
            System.out.println("----------------------------------------");

            //
            // Getting all children of the root named city
            //
            List<Element> cities = root.getChildren("city");
            for (Element city : cities) {
                System.out.println(city.getAttribute("name").getValue());
            }
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats