In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to parse dom4j and solve the problem of garbled code when writing XML documents, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, hope you can get something.
Book.xml
Java Employment basic course Zhang Xiaoxiang 48 yuan 58 yuan JavaScript web page development Li Huoming 46 yuan
Dom4j.java
Package yyy.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test Public class Dom4j {private File file = new File ("src/book.xml"); / / traverse the xml document @ Test public void testList () throws Exception {SAXReader reader = new SAXReader (); Document document = reader.read (file); Element root = document.getRootElement (); list (root);} public void list (Element element) {System.out.println (element.getName ()); List nodes = element.elements () / / System.out.println (nodes.size ()); for (Node node: nodes) {if (node instanceof Element) {list ((Element) node);} / / read the value of a node @ Test public void read () throws Exception {SAXReader reader = new SAXReader (); Document document = reader.read (file); Element root = document.getRootElement () Element price = root.element ("book"). Element ("author"); String value = price.getText (); System.out.println (value);} / / add a node @ Test public void add () throws Exception {SAXReader reader = new SAXReader (); Document document = reader.read (file); Element price = DocumentHelper.createElement ("selling price"); / / create a new node price.setText ("44 yuan") / / set node values Element root = document.getRootElement (); root.element ("book") .add (price); / / add nodes / / rewrite document objects in memory into xml documents pay special attention to garbled problems / / method 1: use OutputStreamWriter settings to write the coding table OutputStreamWriter out = new OutputStreamWriter (new FileOutputStream (file), "utf-8") [xss_clean] (out); out.close (); / * / method 2: OutputFormat format = OutputFormat.createPrettyPrint (); format.setEncoding ("gb2312"); XMLWriter writer = new XMLWriter (new FileWriter (file), format); writer.write (document); writer.close (); * / * / / method 3: OutputFormat format = OutputFormat.createPrettyPrint () XMLWriter writer = new XMLWriter (new OutputStreamWriter (new FileOutputStream (file), "utf-8"), format); writer.write (document); writer.close (); * / * / / method 4: OutputFormat format = OutputFormat.createPrettyPrint (); format.setEncoding ("gb2312"); / / the source document is encoded with "gb2312" without changing the encoding format of the source document or XMLWriter writer = new XMLWriter (new FileOutputStream (file), format). Writer.write (document); writer.close (); * / * / / document objects exist in memory in the form of "UTF-8" encoding. Using FileWriter to write document objects into xml documents in the form of character streams is encoded with local code table "gb2312" by default * garbled summary: * 1. The problem of garbled code should be considered when writing data in a character flow file, but there is no need to consider the problem of garbled code in byte flow * writing data in a character flow file defaults to the local code table "gb2312" * 2. Any object read into memory exists in the form of "UTF-8" encoding * by default, the write method of XMLWriter passes the document object in memory to FileWriter in the form of "UTF-8" encoding, so to avoid garbled problems, * use the wrapper stream OutputStreamWriter and give the coding table used when writing to the file. Or use the setEncoding method of OutputFormat to specify the encoding format to be used when passing to the * stream object. * * /} / / add a node @ Test public void add2 () throws Exception {SAXReader reader = new SAXReader (); Document document = reader.read (file); Element root = document.getRootElement (); Element price = DocumentHelper.createElement ("selling price"); price.setText ("48 yuan"); List list = root.element ("book"). Elements (); list.add (2) OutputFormat format = OutputFormat.createPrettyPrint (); XMLWriter writer = new XMLWriter (new FileOutputStream (file), format); writer.write (document); writer.close ();} / Delete a node @ Test public void delete () throws Exception {SAXReader reader = new SAXReader (); Document document = reader.read (file); Element root = document.getRootElement (); Element price = root.element ("Book"). Element ("Price") Price.getParent () .remove (price); OutputFormat format = OutputFormat.createPrettyPrint (); XMLWriter writer = new XMLWriter (new FileOutputStream (file), format); writer.write (document); writer.close ();} / / change the content of a node @ Test public void update () throws Exception {SAXReader reader = new SAXReader (); Document document = reader.read (file); Element root = document.getRootElement () Element price = root.element ("book"). Element ("selling price"); price.setText ("58 yuan"); OutputFormat format = OutputFormat.createPrettyPrint (); XMLWriter writer = new XMLWriter (new FileOutputStream (file), format); writer.write (document); writer.close ();}} will it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.