1. Basic introduction
XML (extensible markup language, eXtensible Markup Language) is a markup language used to store and transfer data, released by W3C (World Wide Web Alliance) in 1998. XML is designed to provide a simple, extensible and self-described data format so that data can be easily transferred and shared between different systems and applications.
2. Basic characteristics
1. Scalability:
XML allows users to define their own tags (tags), so they can flexibly represent various data types as needed, rather than relying on predefined tags.
2, self-descriptive:
The XML document itself contains information about data and its structure, so that both humans and machines can understand the meaning of data.
3, Structured:
XML documents organize data through hierarchical structures (tree structures), with parent-child relationships, which facilitates the representation of complex data structures.
4, Cross-platform:
XML is a text format that easily transfers and shares data between different systems and applications.
5, Compatibility:
XML is compatible with many other technologies, including HTML, XSLT, JSON, etc., and is widely used in Web services, data exchange and other fields.
3. Basic operations
1. Read XML files
Read XML files using DocumentBuilderFactory and DocumentBuilder:
import ; import ; import org.; import org.; import org.; import ; public class XMLReader { public static void main(String[] args) throws Exception { File xmlFile = new File("path/to/your/"); DocumentBuilderFactory dbFactory = (); DocumentBuilder dBuilder = (); Document doc = (xmlFile); // Get the root node ().normalize(); ("Root element :" + ().getNodeName()); // traverse nodes NodeList nList = ("*"); for (int temp = 0; temp < (); temp++) { Node nNode = (temp); ("\nNode #" + temp + " [" + () + "]:"); if (() == Node.TEXT_NODE) { String nodeValue = ().trim(); if (() > 0) { ("Node Value: " + nodeValue); } } } } }
2. Create an XML file
Create an XML file using DocumentBuilderFactory:
import ; import ; import org.; import org.; public class XMLWriter { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbFactory = (); DocumentBuilder dBuilder = (); Document doc = (); // Create root element Element rootElement = ("Root"); (rootElement); // Create child elements Element childElement = ("Child"); (("Value")); (childElement); // Save to file TransformerFactory transformerFactory = (); Transformer transformer = (); (, "yes"); (new DOMSource(doc), new StreamResult(new File("path/to/your/"))); } }
3. Modify XML files
Modify elements in the XML file:
import ; import ; import org.; import org.; import org.; import org.; import ; import ; import ; import ; import ; public class XMLModifier { public static void main(String[] args) throws Exception { File xmlFile = new File("path/to/your/"); DocumentBuilderFactory dbFactory = (); DocumentBuilder dBuilder = (); Document doc = (xmlFile); // Modify element values NodeList nList = ("Child"); for (int temp = 0; temp < (); temp++) { Node nNode = (temp); if (() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; ("New Value"); } } // Add new elements Element newChild = ("NewChild"); (("New Value")); ().appendChild(newChild); // Delete elements nList = ("Child"); if (() > 1) { Node removeNode = (0); ().removeChild(removeNode); } // Save and modify TransformerFactory transformerFactory = (); Transformer transformer = (); (, "yes"); (new DOMSource(doc), new StreamResult(new File("path/to/your/"))); } }
4. Query XML using XPath
import ; import ; import org.; import org.; import ; import ; import ; import ; public class XPathExample { public static void main(String[] args) throws Exception { File xmlFile = new File("path/to/your/"); DocumentBuilderFactory dbFactory = (); DocumentBuilder dBuilder = (); Document doc = (xmlFile); XPathFactory xpf = (); XPath xpath = (); NodeList nodeList = (NodeList) ("//Child", doc, ); for (int i = 0; i < (); i++) { ((i).getTextContent()); } } }
5. Things to note
Make sure that the necessary libraries and dependencies are introduced into the project.
When processing XML, considering the format and structure of the XML, ensure that the code can correctly access and modify XML elements.
After modifying the XML file, remember to save the changes.
Exception handling is a very important part of handling XML files, ensuring that your code can handle possible exceptions properly.
This is the end of this article about a brief analysis of XML file processing in Java. For more related Java XML file processing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!