In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.