Use Dom4j to parse xml strings and dom4jxml strings
XML file
1 Parsing Code:
1 package xml; 2 3 import java. io. file; 4 import java. util. hashMap; 5 import java. util. iterator; 6 import java. util. map; 7 8 import org. dom4j. document; 9 import org. dom4j. extends entexception; 10 import org. dom4j. export enthelper; 11 import org. dom4j. element; 12 import org. dom4j. io. SAXReader; 13 14 public class TestDom4j {15 public void readStringXml (String xml) {16 Document doc = null; 17 try {18 // read and parse XML documents. SAXReader is a pipe, use a stream to read the xml file 19 SAXReader reader = new SAXReader (); 20 doc = reader. read (new File (xml); 21 22 Element rootElt = doc. getRootElement (); // get the root node 23 System. out. println ("root node:" + rootElt. getName (); // get the root node name 24 25 Iterator <Element> iter = rootElt. elementIterator ("head"); // obtain the child node head 26 27 under the root node // traverse head node 28 while (iter. hasNext () {29 Element recordEle = (Element) iter. next (); 30 String title = recordEle. elementTextTrim ("title"); // get the title value of the subnode under the head node 31 System. out. println ("title:" + title); 32 33 Iterator <Element> iters = recordEle. elementIterator ("script"); // obtain the subnode script 34 35 Under the subnode head // traverse Header node 36 while (iters. hasNext () {37 Element itemEle = (Element) iters. next (); 38 39 String username = itemEle. elementTextTrim ("username"); // get the username value of the byte point in the script of the subnode under the head: 40 String password = itemEle. elementTextTrim ("password"); 41 42 System. out. println ("username:" + username); 43 System. out. println ("password:" + password); 44} 45} 46 Iterator <Element> iterss = rootElt. elementIterator ("body"); // obtain the child node body under the root node 47 // traverse the body node 48 while (iterss. hasNext () {49 Element recordEless = (Element) iterss. next (); 50 String result = recordEless. elementTextTrim ("result"); // get the result value of the subnode under the body node 51 System. out. println ("result:" + result); 52 53 Iterator <Element> itersElIterator = recordEless. elementIterator ("form"); // obtain the child node form 54 under the child node body // traverse form node 55 while (itersElIterator. hasNext () {56 Element itemEle = (Element) itersElIterator. next (); 57 58 String banlce = itemEle. elementTextTrim ("banlce"); // obtain the banlce value of the subnode in the form under the body, 59 String subID = itemEle. elementTextTrim ("subID"); 60 61 System. out. println ("banlce:" + banlce); 62 System. out. println ("subID:" + subID); 63} 64} 65} catch (incluentexception e) {66 e. printStackTrace (); 67} catch (Exception e) {68 e. printStackTrace (); 69} 70} 71 72/** 73 * @ description converts an xml String to map 74 * @ param xml 75 * @ return Map 76 */77 public static Map <String, string> readStringXmlOut (String xml) {78 Map <String, String> map = new HashMap <String, String> (); 79 Document doc = null; 80 try {81 // convert the string to XML 82 doc = DocumentHelper. parseText (xml); 83 // get the root node 84 Element rootElt = doc. getRootElement (); 85 // get the root node name 86 System. out. println ("root node =" + rootElt. getName (); 87 88 // get the child node head 89 Iterator under the root node <Element> iter = rootElt. elementIterator ("head"); 90 // traverse head node 91 while (iter. hasNext () {92 93 Element recordEle = (Element) iter. next (); 94 // get the title value of the subnode under the head node 95 String title = recordEle. elementTextTrim ("title"); 96 map. put ("title", title); 97 // obtain the subnode script 98 Iterator <Element> iters = recordEle. elementIterator ("script"); 99 // traverses the Response node 100 while (iters. hasNext () {101 Element itemEle = (Element) iters. next (); 102 // get the username value of the byte point in the script of the subnode under the head: 103 String username = itemEle. elementTextTrim ("username"); 104 String password = itemEle. elementTextTrim ("password"); 105 106 map. put ("username", username); 107 map. put ("password", password); 108} 109} 110 111 // obtain the subnode body112 Iterator <Element> iterss = rootElt under the root node. elementIterator ("body"); 113 // traverse the body node 114 while (iterss. hasNext () {115 Element recordEless = (Element) iterss. next (); 116 // get the result value of the subnode under the body node 117 String result = recordEless. elementTextTrim ("result"); 118 // obtain the subnode form119 Iterator <Element> itersElIterator = recordEless under the subnode body. elementIterator ("form"); 120 // traverses the Response node 121 while (itersElIterator. hasNext () {122 Element itemEle = (Element) itersElIterator. next (); 123 // get the banlce value of the byte point of the child node form under the body: 124 String banlce = itemEle. elementTextTrim ("banlce"); 125 String subID = itemEle. elementTextTrim ("subID"); 126 127 map. put ("result", result); 128 map. put ("banlce", banlce); 129 map. put ("subID", subID); 130} 131} 132} catch (incluentexception e) {133 e. printStackTrace (); 134} catch (Exception e) {135 e. printStackTrace (); 136} 137 return map; 138} 139 140 public static void main (String [] args) {141 142 // below is an xml String to be parsed example 143 String xmlString = "E: // xml // hbm. xml value: root node: htmltitle: dom4j parse the value of username: yangrongpassword: 123456 result: 0 banlce: 1000 subID: 36242519880716XML string xmlString: root Node = htmlresult = 0 username = yangrongtitle = dom4j parse an example of subID = 36242519880716 banlce = 1000 password = 123456
How to Use DOM4J in JAVA to parse a string into XML format
Usage:
DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
Document doc = factory. newDocumentBuilder (). parse (new ByteArrayInputStream (xmlStr. getBytes ()));
Try.
You cannot use xpath to parse xml strings using dom4j.
Hahahaha, less jaxme-api-0.3.jar package, FAQs!