import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import java.io.File; public class XmlParserExample { public static void main(String[] args) { try { File xmlFile = new File("example.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); // Access and manipulate document content here } catch (Exception e) { e.printStackTrace(); } } }
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Attr; public class XmlCreatorExample { public static void main(String[] args) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.newDocument(); // Create root element Element rootElement = doc.createElement("root"); doc.appendChild(rootElement); // Create child element with attribute Element childElement = doc.createElement("child"); Attr attr = doc.createAttribute("attribute"); attr.setValue("value"); childElement.setAttributeNode(attr); rootElement.appendChild(childElement); // Save document to file or print to console } catch (Exception e) { e.printStackTrace(); } } }In both examples above, the package library used is javax.xml.parsers.