Using Stax to manipulate XML is very simple, and its reading process is like a cursor moving. Different processing is done for different nodes.
Let's look at a cursor-based model for XML processing:
public class Staxtest {@Test public void test1 () {try {//1.Build Xmlstreamreader xmlinputfactory factory = xmlinputfactory. newinstance();Xmlstreamreader reader = Factory. Createxmlstreamreader(Staxtest. Class . getResourceAsStream("books.xml"));while (reader. Hasnext()) {int type = Reader. Next();Is the start node, the start node is <> if (type==xmlstreamreader. START_element) {System. out. println("<"+reader. GetName()+">");Whether it is a text node, the content between the start node and the end node}else if (type==xmlstreamreader. Characters) {System. out. println(Reader. GetText());Is the end node, the end node is </>}else if (type==xmlstreamreader. END_element) {System. out. println("</"+reader. GetName()+">");}}} catch (Factoryconfigurationerror | Xmlstreamexception e) {E. Printstacktrace();}} @Test public void Test2 () {try {//1.Build Xmlstreamreader xmlinputfactory factory = xmlinputfactory. newinstance();Xmlstreamreader reader = Factory. Createxmlstreamreader(Staxtest. Class . getResourceAsStream("books.xml"));while (reader. Hasnext()) {int type = Reader. Next();Outputs the properties of all book nodes,0Represents the number of arguments if (type==xmlstreamreader. START_element) {if (reader. GetName(). toString(). Equals("book")) {System. out. println(Reader. Getattributename(0) +":"+ Reader. Getattributevalue(0));}}}} catch (Factoryconfigurationerror | Xmlstreamexception e) {E. Printstacktrace();}} @Test public void Test3 () {try {//1.Build Xmlstreamreader xmlinputfactory factory = xmlinputfactory. newinstance();Xmlstreamreader reader = Factory. Createxmlstreamreader(Staxtest. Class . getResourceAsStream("books.xml"));while (reader. Hasnext()) {int type = Reader. Next();Output the name and price of each book, if (Type==xmlstreamreader. START_element) {if (reader. GetName(). toString(). Equals("Name")) {System. out. Print(Reader. GetName()+":"+reader. Getelementtext());} if (reader. GetName(). toString(). Equals("Price")) {System. out. println(Reader. GetName()+":"+reader. Getelementtext());}}}} catch (Factoryconfigurationerror | Xmlstreamexception e) {E. Printstacktrace();} }}
<books> < book category="Programming Technology" Edition="8"> <ID>1</ID> <name>Java Programming Ideas</name> <price >80</price > <author>Tom</author> <detail> <presstime>Celestial</presstime> <storytime>21st Century</storytime> </Detail> </book > < book category="historical novel" edition="1"> <ID>2</ID> <name>Three kingdoms</name> <price >30</price > <author>Luo Guan Zhong</author> <detail> <presstime>Ming dynasty</presstime> <storytime>End of Han Dynasty</storytime> </Detail> </book > < book category="novel" edition="2"> <ID>3</ID> <name>Dream</name> <price >35</price > <author>Cao Xueqin</author> <detail> <presstime>Qing dynasty</presstime> <storytime>Unknown</storytime> </Detail> </book > < book category="myth novel" edition="4"> <ID>4</ID> <name>Journey</name> <price >25</price > <author>Wu Chengen</author> <detail> <presstime>Ming dynasty</presstime> <storytime>Datang</storytime> </Detail> </book > < book category="novel" edition="5"> <ID>5</ID> <name>Outlaws</name> <price >30</price > <author>Shi Naian</author> <detail> <presstime>Ming dynasty</presstime> <storytime>Great song</storytime> </Detail> </book ></Books>
There is also a processing method based on the iterative model, based on the iterative model and based on the cursor model is very similar to see the code:
@Test Public void test4(){Try{Xmlinputfactory factory = Xmlinputfactory.newinstance (); Xmleventreader reader = Factory.createxmleventreader (StaxTest.class.getResourceAsStream ("books.xml")); while(Reader.hasnext ()) {xmleventEvent= Reader.nextevent ();if(Event. Isstartelement ()) {System. out. println ("<"+Event. Asstartelement (). GetName () +">"); }Else if(Event. Ischaracters ()) {System. out. println (Event. Ascharacters (). GetData ()); }Else if(Event. Isendelement ()) {System. out. println ("</"+Event. Asendelement (). GetName () +">"); } } }Catch(Factoryconfigurationerror | Xmlstreamexception e) {e.printstacktrace (); } }
This piece of code output is exactly the same as the test1 () output, based on the iterative model, which is to first event.as****, and then the operation is exactly the same as the cursor-based model.
Then look at adding filters to the iteration model,
@Test Public void Test5() {Try{Xmlinputfactory factory = Xmlinputfactory.newinstance (); Xmleventreader reader = Factory.createfilteredreader (factory. Createxmleventreader (Staxtest.class . getResourceAsStream ("books.xml")), (Event), {if(Event. Isstartelement ())return true;return false; }); while(Reader.hasnext ()) {xmleventEvent= Reader.nextevent (); System. out. println (Event. Asstartelement (). GetName ()); } }Catch(Factoryconfigurationerror | Xmlstreamexception e) {e.printstacktrace (); } }
The above section adds a filter to the iteration model, which uses the JDK8 's newest feature lambda expression to simplify the operation, and the filter returns all the start nodes.
Introduction to Java Stax operation XML