Sometimes we need to read and write XML, the common XML operations are mainly streams and Dom way.
One. Streams Way
The streams commonly used classes are two Xmlstreamreader and Xmlstreamwriter.
Xmlstreamreader: The reading of this reading mode is read from top to bottom, and is based on reader's EventType top-down running steps.
We will wrap this XML into a goods list, goods includes the item,name and the type attribute, with the following code:
/** Assume that the current XML data style is: *<?xml version= "1.0"? >*<goodslist>* <goods item= "1" >* <name> Huawei Mobile </ name>* <type> Huawei </type>* </goods>* <goods item= "2" >* <name> \phone </na me>* <type> </type>* </goods>*</goodslist>* need to parse XML into a list of goods*/ Public classXmlreadercontroller { Public classGoods { PublicString Item{get;set;} PublicString Name{get;set;} PublicString Type{get;set;} } PublicList<goods>getgoodslistbyxmlfile (String goodsxml) {Xmlstreamreader reader=NewXmlstreamreader (Goodsxml); Boolean Flagxmlend=true; List<Goods> goodslist =NewList<goods>(); while(flagxmlend) {Goods tempgoods; if(Reader.geteventtype () = =xmltag.start_element) { if(Reader.getlocalname (). Equalsignorecase (' goods ')) ) {Tempgoods=getgoods (reader); } } if(Reader.hasnext ()) {reader.next (); } Else{flagxmlend=false; Break; } if(Tempgoods! =NULL) {goodslist.add (tempgoods); } } returngoodslist; } Goods Getgoods (Xmlstreamreader reader) {Goods tempgoods=NewGoods (); Tempgoods.item= Reader.getattributevalue (NULL, ' Item '); Boolean Flagisloop=true; while(flagisloop) {if(Reader.hasnext ()) {reader.next (); if(Reader.geteventtype () = =xmltag.start_element) { if(Reader.getlocalname (). Equalsignorecase (' name ') ) {reader.next (); Tempgoods.name=Reader.gettext (); } Else if(Reader.getlocalname (). Equalsignorecase (' type ')) ) {reader.next (); Tempgoods.type=Reader.gettext (); } } if(Reader.geteventtype () = = Xmltag.end_element && reader.getlocalname (). Equalsignorecase (' goods ') ) {Flagisloop=false; Break; } } Else{Flagisloop=false; Break; } } returnTempgoods; }}
In the anonymous block test method:
String goodsxml = ' <?xml version= ' 1.0 '?> ' + ' <goodsList> ' + ' <goods item= ' 1 ' > ' + ' < name> Huawei Mobile </name> ' + ' <type> Huawei </type> ' + ' </goods> ' + ' <goods item= "2" > ' + ' <name> phone </name> ' + ' <type> </type> ' + ' </goods> ' + ' </goodsList> '; ListNew Xmlreadercontroller (). Getgoodslistbyxmlfile (Goodsxml); System.debug (Json.serialize (goodslist));
Show Results:
[{"Type": "Huawei", "name": "Huawei Phone", "Item": "1"},{"type": "", "name": " Phone", "Item": "2"}
Xmlstreamwriter: process with Xmlstreamreader, need to write from top to bottom, for example, if you write the above XML file, you need to startdocument first, and then startelement ... Note that each start needs to correspond to the corresponding end method.
Public classXmlwritercontroller { Public Static voidwriteXml () {xmlstreamwriter writer=NewXmlstreamwriter (); Writer.writestartdocument (' Utf-8 ', ' 1.0 '); Writer.writecomment (' Goodslist start Here '); Writer.writestartelement (', ' goodslist ', ' http://www.goods.com '); Writer.writenamespace ("', ' http://www.goods.com '); Writer.writestartelement (NULL, ' goods ',NULL); Writer.writeattribute (NULL,NULL, ' item ', ' 1 '); Writer.writestartelement (NULL, ' name ',NULL); Writer.writecharacters (' Huawei Mobile '); Writer.writeendelement (); Writer.writestartelement (NULL, ' type ',NULL); Writer.writecharacters (Huawei); Writer.writeendelement (); Writer.writeendelement (); Writer.writestartelement (NULL, ' goods ',NULL); Writer.writeattribute (NULL,NULL, ' Item ', ' 2 '); Writer.writestartelement (NULL, ' name ',NULL); Writer.writecharacters ( phone '); Writer.writeendelement (); Writer.writestartelement (NULL, ' type ',NULL); Writer.writecharacters (Millet); Writer.writeendelement (); Writer.writeendelement (); Writer.writeendelement (); Writer.writeenddocument (); System.debug (Writer.getxmlstring ()); }}
Two. Dom parsing
Dom parsing principle and Java for Dom parsing the same, here, goodslist as the root node, goodslist child nodes have goods1,goods. They have attributes Item1 and ITEM2,GOODS1 and Goods2, respectively, with corresponding child nodes.
The above XML is parsed into a list of goods using the DOM method.
Public classDomxmlcontroller { Public classGoods {String item{get;set;} String Name{get;set;} String Type{get;set;} } PublicList<goods>getgoodsviaxmldom (String xmlstring) {dom.document Document=Newdom.document (); Document.load (xmlstring); Dom.xmlnode rootelement=document.getrootelement (); List<Goods> goodslist =NewList<goods>(); for(Dom.xmlnode node:rootElement.getChildElements ()) {if(Node.getname (). Equalsignorecase (' goods ')) {Goods tempgoods=NewGoods (); Tempgoods=getgoodsnameandtype (node); Tempgoods.item= Node.getattribute (' Item ',NULL); Goodslist.add (Tempgoods); } } returngoodslist; } Goods Getgoodsnameandtype (Dom.xmlnode parentnode) {transientGoods Tempgoods =NewGoods (); for(Dom.xmlnode node:parentNode.getChildElements ()) {if(Node.getname (). Equalsignorecase (' name ') ) {Tempgoods.name=Node.gettext (); } Else if(Node.getname (). Equalsignorecase (' type ')) ) {Tempgoods.type=Node.gettext (); } } returnTempgoods; }}
The anonymous block test content is as follows:
String goodsxml = ' <?xml version= ' 1.0 '?> ' + ' <goodsList> ' + ' <goods item= ' 1 ' > ' + ' < name> Huawei Mobile </name> ' + ' <type> Huawei </type> ' + ' </goods> ' + ' <goods item= "2" > ' + ' <name> phone </name> ' + ' <type> </type> ' + ' </goods> ' + ' < /goodslist> '; System.debug (Json.serialize (new Domxmlcontroller (). Getgoodsviaxmldom (Goodsxml)));
Show Results:
Summary:apex is similar to Java for XML operations, or most of it comes from Java, and if the Java parsing XML is very skilful, using Apex parsing XML is just a matter of looking at the method. This article simply describes the simplest XML operations, many of which are not used in this article, are interested in or want to go deep to see the relevant API.
Salesforce 0 Basic Learning (32) Read and write XML through streams and DOM