// Load the XML file into a XmlObject XmlObject xmlObj = XmlObject.Factory.parse(new File("example.xml")); // Access elements in the XML document String firstName = xmlObj.selectPath("/person/firstName/text()")[0].getDomNode().getNodeValue(); String lastName = xmlObj.selectPath("/person/lastName/text()")[0].getDomNode().getNodeValue(); // Print the values System.out.println(firstName + " " + lastName);
// Create a new XML document XmlObject xmlObj = XmlObject.Factory.newInstance(); // Create a new element XmlCursor cursor = xmlObj.newCursor(); cursor.toNextToken(); cursor.beginElement("person"); cursor.insertAttributeWithValue("id", "1"); cursor.insertElementWithText("firstName", "John"); cursor.insertElementWithText("lastName", "Doe"); cursor.dispose(); // Print the XML document System.out.println(xmlObj.xmlText());This code is an example of how to create a new XML document using XmlBeans and add elements to it. The cursor is used to navigate the document and create new elements with values. Finally, the XML document is printed to the console.