Import Java. io. stringreader; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. export enthelper; import Org. dom4j. element; import Org. dom4j. io. saxreader; public class Testa {public static string test () {document = incluenthelper. createdocument (); element = document. addelement ("aaaa"); element ele1 = element. addelement ("BBBB"); element ele2 = element. addelement ("CCCC"); ele1.addtext ("1"); ele2.addtext ("hello"); Return document. asxml ();} public static void main (string [] ARGs) {string test = test (); system. out. println (TEST); stringreader = new stringreader (TEST); saxreader reader = new saxreader (); document DOC = NULL; try {Doc = reader. read (stringreader);} catch (incluentexception e) {e. printstacktrace ();} element = Doc. getrootelement (); string resultcode = element. elementtext ("BBBB"); string resultmsg = element. elementtext ("CCCC"); system. err. println ("[AAAA]" + resultcode); system. err. println ("[BBBB]" + resultmsg );}}
Result:
<? XML version = "1.0" encoding = "UTF-8"?> <AAAA> <BBBB> 1 </BBBB> <CCCC> Hello </CCCC> </AAAA> [AAAA] 1 [BBBB] Hello
Document --> XML message transmission and parsing, simple example