


Java example code for generating and parsing XML format files and strings
1. Basic knowledge:
There are generally four methods for Java to parse XML: DOM, SAX, JDOM, and DOM4J.
2. Introduction to use
1), Introduction to DOM
(1)
The interface provided by W3C (org.w3c.dom), which reads the entire XML document Memory, build a DOM tree to operate each node (Node). The advantage is that the entire document is always in memory, we can access any node at any time, and tree traversal is also a relatively familiar operation; the disadvantage is that it consumes memory, and we must wait until all documents are read into memory before processing.
(2) Sample code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <root> <TelePhone> <type name="nokia"> <price>599</price> <operator>CMCC</operator> </type> <type name="xiaomi"> <price>699</price> <operator>ChinaNet</operator> </type> </TelePhone> </root>
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class XMLHandler { public XMLHandler(){ } public String createXML(){ String xmlStr = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); document.setXmlVersion("1.0"); Element root = document.createElement("root"); document.appendChild(root); Element telephone = document.createElement("TelePhone"); Element nokia = document.createElement("type"); nokia.setAttribute("name", "nokia"); Element priceNokia = document.createElement("price"); priceNokia.setTextContent("599"); nokia.appendChild(priceNokia); Element operatorNokia = document.createElement("operator"); operatorNokia.setTextContent("CMCC"); nokia.appendChild(operatorNokia); telephone.appendChild(nokia); Element xiaomi = document.createElement("type"); xiaomi.setAttribute("name", "xiaomi"); Element priceXiaoMi = document.createElement("price"); priceXiaoMi.setTextContent("699"); xiaomi.appendChild(priceXiaoMi); Element operatorXiaoMi = document.createElement("operator"); operatorXiaoMi.setTextContent("ChinaNet"); xiaomi.appendChild(operatorXiaoMi); telephone.appendChild(xiaomi); root.appendChild(telephone); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transFormer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(document); //export string ByteArrayOutputStream bos = new ByteArrayOutputStream(); transFormer.transform(domSource, new StreamResult(bos)); xmlStr = bos.toString(); //------- //save as file File file = new File("TelePhone.xml"); if(!file.exists()){ file.createNewFile(); } FileOutputStream out = new FileOutputStream(file); StreamResult xmlResult = new StreamResult(out); transFormer.transform(domSource, xmlResult); //-------- } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return xmlStr; } public void parserXML(String strXML){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); StringReader sr = new StringReader(strXML); InputSource is = new InputSource(sr); Document doc = builder.parse(is); Element rootElement = doc.getDocumentElement(); NodeList phones = rootElement.getElementsByTagName("type"); for (int i = 0; i < phones.getLength(); i++) { Node type = phones.item(i); String phoneName = ((Element)type).getAttribute("name"); System.out.println("Phone name = "+phoneName); NodeList properties = type.getChildNodes(); for (int j = 0; j < properties.getLength(); j++) { Node property = properties.item(j); String nodeName = property.getNodeName(); if (nodeName.equals("price")) { String price=property.getFirstChild().getNodeValue(); System.out.println("price="+price); } else if (nodeName.equals("operator")) { String operator=property.getFirstChild().getNodeValue(); System.out.println("operator="+operator); } } } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { XMLHandler handler = new XMLHandler(); String xml = handler.createXML(); System.out.println(xml); handler.parserXML(xml); } }
(3) The difference between element (Element) and node (Node) (org.w3c.dom)
Node object is the entire document object The main data type of the model is the most basic object in the DOM and represents the abstract node in the document tree. However, in actual use, the Node object is rarely used directly. Instead, the sub-objects Element, Attr, Text, etc. of the Node object are used.
The Element object represents an element in an HTML or XML document and is the main sub-object of the Node class. The element can contain attributes, so Element has methods for accessing its attributes.
Element is inherited from Node. Element is a small-scale definition. It must be a node containing complete information to be an element, such as
node has several subtypes: Element, Text, Attribute, RootElement, Comment, Namespace, etc.
2), SAX
3), JDOM
4), DOM4J
(1) Introduction
dom4j is currently the best in xml parsing (Hibernate and Sun's JAXM also use dom4j to parse XML). It incorporates many features beyond the basic XML document representation. Features include integrated XPath support, XML Schema support, and event-based processing for large or streaming documents.
When using XPATH, add jaxen.jar, otherwise the following error will occur:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230) at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207) at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
(2) Sample code:
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.xml.sax.InputSource; public class XMLHandler { public XMLHandler() { // TODO Auto-generated constructor stub } public String createXML(){ String strXML = null; Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); Element phone = root.addElement("TelePhone"); Element nokia = phone.addElement("type"); nokia.addAttribute("name", "nokia"); Element price_nokia = nokia.addElement("price"); price_nokia.addText("599"); Element operator_nokia = nokia.addElement("operator"); operator_nokia.addText("CMCC"); Element xiaomi = phone.addElement("type"); xiaomi.addAttribute("name", "xiaomi"); Element price_xiaomi = xiaomi.addElement("price"); price_xiaomi.addText("699"); Element operator_xiaomi = xiaomi.addElement("operator"); operator_xiaomi.addText("ChinaNet"); //-------- StringWriter strWtr = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter xmlWriter =new XMLWriter(strWtr, format); try { xmlWriter.write(document); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } strXML = strWtr.toString(); //-------- //------- //strXML=document.asXML(); //------ //------------- File file = new File("TelePhone.xml"); if (file.exists()) { file.delete(); } try { file.createNewFile(); XMLWriter out = new XMLWriter(new FileWriter(file)); out.write(document); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //-------------- return strXML; } public void parserXML(String strXML){ SAXReader reader = new SAXReader(); StringReader sr = new StringReader(strXML); InputSource is = new InputSource(sr); try { Document document = reader.read(is); Element root = document.getRootElement(); //get element List<Element> phoneList = root.elements("TelePhone"); List<Element> typeList = phoneList.get(0).elements("type"); for (int i=0;i<typeList.size();i++){ Element element = typeList.get(i); String phoneName = element.attributeValue("name"); System.out.println("phonename = "+phoneName); //get all element List<Element> childList = element.elements(); for (int j=0;j<childList.size();j++){ Element e = childList.get(j); System.out.println(e.getName()+"="+e.getText()); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void parserXMLbyXPath(String strXML){ SAXReader reader = new SAXReader(); StringReader sr = new StringReader(strXML); InputSource is = new InputSource(sr); try { Document document = reader.read(is); List list = document.selectNodes("/root/TelePhone/type"); for(int i=0;i<list.size();i++){ Element e = (Element) list.get(i); System.out.println("phonename="+e.attributeValue("name")); List list1 = e.selectNodes("./*"); for(int j=0;j<list1.size();j++){ Element e1 = (Element) list1.get(j); System.out.println(e1.getName()+"="+e1.getText()); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub XMLHandler handler = new XMLHandler(); String strXML=handler.createXML(); System.out.println(strXML); handler.parserXML(strXML); System.out.println("-----------"); handler.parserXMLbyXPath(strXML); } }
5)XPATH
(1) Introduction
XPath is a language for finding information in XML documents. XPath is used to navigate through elements and attributes in XML documents.
Reference for specific syntax introduction: http://w3school.com.cn/xpath/index.asp
(2) Sample code:
import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class XMLHandler { public XMLHandler() { // TODO Auto-generated constructor stub } public void parserXML(String strXML){ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); StringReader sr = new StringReader(strXML); InputSource is = new InputSource(sr); Document doc = builder.parse(is); XPathFactory xFactory = XPathFactory.newInstance(); XPath xpath = xFactory.newXPath(); XPathExpression expr = xpath.compile("/root/TelePhone/type"); NodeList phones = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < phones.getLength(); i++) { Node type = phones.item(i); String phoneName = ((Element)type).getAttribute("name"); System.out.println("Phone name = "+phoneName); XPathExpression expr1 = xpath.compile("./*"); NodeList list = (NodeList) expr1.evaluate(type, XPathConstants.NODESET); for(int j =0;j<list.getLength();j++){ Element e1 = (Element) list.item(j); System.out.println(e1.getNodeName()+"="+e1.getTextContent()); } } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (XPathExpressionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String strXML="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"+ "<root>"+ "<TelePhone>"+ "<type name=\"nokia\">"+ "<price>599</price>"+ "<operator>CMCC</operator>"+ "</type>"+ "<type name=\"xiaomi\">"+ "<price>699</price>"+ "<operator>ChinaNet</operator>"+ "</type>"+ "</TelePhone>"+ "</root>"; XMLHandler handler = new XMLHandler(); handler.parserXML(strXML); } }
More Java generation and parsing XML formats For articles related to file and string example codes, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Methods to ensure the security of XML/RSSfeeds include: 1. Data verification, 2. Encrypted transmission, 3. Access control, 4. Logs and monitoring. These measures protect the integrity and confidentiality of data through network security protocols, data encryption algorithms and access control mechanisms.

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

How to build, validate and publish RSSfeeds? 1. Build: Use Python scripts to generate RSSfeed, including title, link, description and release date. 2. Verification: Use FeedValidator.org or Python script to check whether RSSfeed complies with RSS2.0 standards. 3. Publish: Upload RSS files to the server, or use Flask to generate and publish RSSfeed dynamically. Through these steps, you can effectively manage and share content.

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.
