Java Pull method to generate and parse xml files, pullxml
Google has already integrated the Pull XML Parser into the android sdk, which is officially recommended by google.
If we want to use the Pull method to generate xml files and parse xml files on the Java Desktop and j2s, we need to use kxml2;
KXML parser is a small parser based on ordinary xml pull parser, the official website is http://kxml.org/
The official website of common xml pull parser is http://xmlpull.org/
Lab started:
Create a new java project in Eclipse, create a new libs folder, copy the kxml2-2.2.2.jar files downloaded from the Internet to it, configure the compilation path;
The xml file style we want to operate on is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Root> <wisdom id = "1"> <content> now, you are dreaming, you will dream of </content> <author> Harvard Library </author> </wisdom> <wisdom id = "2"> <content> today, it is the tomorrow that people pray for yesterday </content> <author> Harvard Library </author> </wisdom> </root>
Each node in the xml file is described using an entity class:
public class Wisdom {private int id;private String content;private String author;public Wisdom() {super();}public Wisdom(String content, String author) {super();this.content = content;this.author = author;}public Wisdom(int id, String content, String author) {super();this.id = id;this.content = content;this.author = author;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}}
The core category has two major functions:
1. Resolution
2. Generate
Import java. io. file; import java. io. fileOutputStream; import java. io. inputStream; import java. util. arrayList; import java. util. list; import org. kxml2.io. KXmlParser; import org. kxml2.io. KXmlSerializer; import org. xmlpull. v1.XmlPullParser; import org. xmlpull. v1.XmlSerializer; /***** @ author Administrator **/public class PullDemo {/***** parse the xml file in the input stream ** @ param is input stream * @ return parse result set */public List <Wisdom> par SeXml (InputStream is) {// declare the returned value List <Wisdom> wisdomList = null; // retrieve the parsed object XmlPullParser xmlPullParser = new KXmlParser (); try {// set the input stream encoding xmlPullParser. setInput (is, "UTF-8"); // retrieves the parsed event type int eventType = xmlPullParser. getEventType (); // declare a Wisdom reference to Wisdom wisdom = null; // determine whether the file is parsed. while (eventType! = XmlPullParser. END_DOCUMENT) {String tagName = xmlPullParser. getName (); switch (eventType) {case XmlPullParser. START_DOCUMENT: wisdomList = new ArrayList <Wisdom> (); break; case XmlPullParser. START_TAG: if ("wisdom ". equals (tagName) {// create the wisdom object wisdom = new Wisdom (); wisdom. setId (Integer. parseInt (xmlPullParser. getAttributeValue (null, "id");} else if ("content ". equals (tagName) {wisdom. setContent (xmlP UllParser. nextText ();} else if ("author ". equals (tagName) {wisdom. setAuthor (xmlPullParser. nextText ();} break; case XmlPullParser. END_TAG: if ("wisdom ". equals (tagName) & wisdom! = Null) {// Add the wisdom object to the set to go to wisdomList. add (wisdom); wisdom = null;} break;} // read the next event eventType = xmlPullParser. next ();} // close the input stream is. close ();} catch (Exception e) {e. printStackTrace ();} return wisdomList ;} /*** generate an xml file based on the content in the List ** @ param wisdomList * in the List of multiple wisdom objects * @ return true indicates that the file is successfully generated, false indicates an error occurred while generating */public boolean createXML (List <Wisdom> wisdomList) {// Use pull resolution for implementation // target file path String filePath = "D: \ wisdoms. xml "; // target File file = new File (filePath); // obtain the xml serialization instance XmlSerializer serializer = new KXmlSerializer (); // file write stream instance FileOutputStream fos = null; try {// create a file output stream object fos = new FileOutputStream (file) based on the file object ); // set the output stream and the encoding serializer. setOutput (fos, "UTF-8"); // sets the start of the file serializer. startDocument ("UTF-8", true); // sets the file start label serializer. startTag (null, "root"); for (Wisdom wisdom: wisdomList) {// start of the wisdom tag serializer. startTag (null, "wisdom"); // you can specify serializer for the attributes of the wisdom tag. attribute (null, "id", wisdom. getId () + ""); // sets the subtag contentserializer of the wisdom tag. startTag (null, "content"); serializer. text (wisdom. getContent (); serializer. endTag (null, "content"); // set the ageserializer of the sub-tag of the wisdom tag. startTag (null, "author"); serializer. text (wisdom. getAuthor (); serializer. endTag (null, "author"); // end of the wisdom tag serializer. endTag (null, "wisdom");} // sets the end tag serializer. endTag (null, "root"); // end of the file serializer. endDocument (); serializer. flush (); fos. close (); return true;} catch (Exception e) {e. printStackTrace (); return false ;}}}
To use the core class in the main method, generate an xml file on drive D (windows operating system), parse the xml file, and print the parsed collection to the console.
Import java. io. file; import java. io. fileInputStream; import java. util. arrayList; import java. util. list; public class PullTest {public static void main (String [] args) {// ************** initialize the List set ******************* ArrayList <Wisdom> wisdomList = new ArrayList <> (); wisdom w = new Wisdom (); w. setId (1); w. setContent ("nap at the moment, you will dream; while learning at the moment, you will dream"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (2); w. se TContent ("I am ruined today, it is the tomorrow that people pray for yesterday"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (3); w. setContent ("when it is too late, it is the first time"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (4); w. setContent ("do not drag today to Tomorrow"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (5); w. setContent ("The pain in learning is temporary, and the pain in not learning is lifelong"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (6); W. setContent ("learning this is not a lack of time, but a lack of effort"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (7); w. setContent ("Happiness may not be ranked, but success will be ranked"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (8); w. setContent ("learning is not all about life. But what else can we do if we can't conquer a part of our lives-learning? "); W. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (9); w. setContent ("Please enjoy the unavoidable pain"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (10); w. setContent ("the taste of success can be tasted only when you work harder and earlier than others"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (11); w. setContent ("no one can succeed at will, it comes from thorough self-management and perseverance"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (12); w. setContent ("time elapsed"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (13); w. setContent ("the current stream of saliva will become tomorrow's tears"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (14); w. setContent ("dogs learn the same thing, gentlemen play the same"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (15); w. setContent ("don't go today, run tomorrow"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (16); w. setContent ("future investors, people loyal to reality"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (17); w. setContent ("educational level indicates income"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (18); w. setContent ("end of a day, will not come again"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (19); w. setContent ("even now, the opponent keeps turning the book page"); w. setAuthor ("Harvard Library"); wisdomList. add (w); w = new Wisdom (); w. setId (20); w. setContent ("no hardships, no gains"); w. setAuthor ("Harvard Library"); wisdomList. add (w ); // ************* initialize the List set **************// create a PullDemo object PullDemo pd = new PullDemo (); // generate the xml file pd. createXML (wisdomList); try {File file = new File ("D: \ wisdoms. xml "); // read the file stream FileInputStream FCM = new FileInputStream (file); // call the xml parsing method to obtain the result set List <Wisdom> list = pd. parseXml (FCM); // print cyclically for (Wisdom wisdom: list) {System. out. println (wisdom. getContent () ;}} catch (Exception e) {System. out. println (e. getMessage ());}}}
The experiment is complete. The jar package for KXML2 is attached here: kml2-2.2.2.jar
I hope the above Code will help you!