Instance Analysis of xml string of string Type in Java
Most people do not understand the knowledge points of this article, "string string instance analysis of string type in Java", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "string string instance analysis of string in Java" article.
Parsing xml strings of type string
I first concatenate a string in xml format, simulate to get such data, and then parse it.
Import the required packages into StringBuilder sb = new StringBuilder (); sb.append ("); sb.append (" + "this is a mark!") + "); sb.append ("); sb.append (""); sb.append ("" + ") to parse me!" + "); sb.append ("); / / start parsing String str= sb.toString (); Document doc = null; try {doc = DocumentHelper.parseText (str) } catch (DocumentException e) {e.printStackTrace ();} Element root = doc.getRootElement (); / / point to the root node try {Element mark=root.element ("header"). Element ("mark"); Element second=root.element ("body"). Element ("data"). Element ("first"). Element ("second") / / get the node content of xml System.out.println (mark.getTextTrim ()); System.out.println (second.getTextTrim ());} catch (Exception e) {e.printStackTrace ();}
Running result:
Parsing String type t complex xml, multi-level node, the best example
Need to use jar to package dom4j-1.6.1.jar
The string xml is as follows: aa 22 10 10.1.1.2 9999 10.1.1.5 717 2 33 10. 1.1.2 9999 10.1.1.9 878 parsing code import java.io.ByteArrayInputStream Import java.io.UnsupportedEncodingException;import java.util.Iterator;import org.dom4j.DocumentException;import org.dom4j.io.SAXReader;public class XMLParser {public static void main (String [] args) {String xml = "aa221010.1.1.2999910.1.1.571723310.1.1.2999910.1.1.9878"; long start = System.currentTimeMillis (); SAXReader reader = new SAXReader () Try {org.dom4j.Document doc = reader.read (new ByteArrayInputStream (xml. GetBytes ("UTF-8")); org.dom4j.Element root = doc.getRootElement (); org.dom4j.Element element; org.dom4j.Element element2; org.dom4j.Element element3 For (Iterator I = root.elementIterator ("flw"); i.hasNext ();) {element = (org.dom4j.Element) i.next (); System.out .println ("name: [" + element.elementText ("name") + "]") System.out.println ("age: [" + element.elementText ("age") + "]"); for (Iterator j = element.elementIterator ("instance_info"); j. HasNext ();) {element2 = (org.dom4j.Element) j.next () System.out.println ("\ tnum: [" + element2.elementText ("num") + "]"); System.out.println ("\ tcode: [" + element2.elementText ("code") + "]") For (Iterator k = element2.elementIterator ("instance"); k .hasNext ();) {element3 = (org.dom4j.Element) k.next () System.out.println ("\ t\ tip: [" + element3.elementText ("ip") + "]"); System.out.println ("\ t\ tmask: [" + element3.elementText ("mask") + "]") } catch (DocumentException e) {e.printStackTrace ();} catch (UnsupportedEncodingException e) {e.printStackTrace ();} long end = System.currentTimeMillis () System.out.println ("time-consuming:" + (end-start) + "ms");}} the above is about "instance analysis of xml string of string type in Java". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.