John 25
import org.jdom2.Element; import org.jdom2.input.SAXBuilder; // Load the XML document SAXBuilder builder = new SAXBuilder(); Element root = builder.build("path/to/document.xml").getRootElement(); // Retrieve the name element String name = root.getChildText("name"); // Print the value of the name element System.out.println("Name: " + name);
Name: John
John 25 Jane 30
import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import java.util.List; // Load the XML document SAXBuilder builder = new SAXBuilder(); Element root = builder.build("path/to/document.xml").getRootElement(); // Retrieve the name elements for each person Listpersons = root.getChildren("person"); for (Element person : persons) { String name = person.getChildText("name"); System.out.println("Name: " + name); }
Name: John Name: JanePackage Library: org.jdom2