XML Extensible Markup Language
1. Concept:
XML was originally intended to replace HTML, tag names can be defined directly, cannot be used to start with a numeric case-sensitive, use a configuration file to encapsulate data
Because XML is well-formed, it is widely used, such as data-exchange data configuration for persistent storage data
the difference between XML and HTML :
1) The HTML tag is fixed and cannot be extended. XML tags are not fixed and extensible.
2) HTML focuses on the information displayed. XML focuses on the structure of identifying information.
3) HTML is case-insensitive. XML is case-sensitive. XML syntax is more rigorous than HTML.
2. Components
Document declaration: <?xml version= "1.0" encoding= "Utf-8"?>
Must top line shelf Write
Root tag: has and has only one root tag
Other Tags: have a start tag must have an end tag
Tag properties: A label can have multiple properties, each with its own name and value, for example: <student name= "Text" >
Annotations:<!---->//comment Annotation objects
tags in the contents of Egg Zhang San:<name> Zhang San </name>
All whitespace and line breaks appear in the XML tag, and the XML parser is treated as a label content
3. Analytic thinking
Dom parsing thought: Document Object Model (DOM4J parsing)
Load the document into memory once and then extract the parts of the document as objects
Pros: Ability to add and revise documents
Disadvantage: Memory consumption is suitable for PC side
Sax parsing thought: Parsing based on events, reading one row at a time, releasing a line (sax parsing, pull parsing)
Pros: No memory available for mobile
Disadvantages: can only be found and deleted changes
Second, dom4j analysis
To read the XML file step:
1. Import dom4j jar Package
2. Creating the Parser Object
Saxreader reader = new Saxreader ();
Document doc = Reader.read (new FileInputStream ("XML file name. xml"));
3. Get the root Tag object
Element rootelement = Doc.getrootelement ();
1)Rootelement.node (the first few nodes);//Get a single Node object
Gets the child node under the root tag (cannot get the grandson node), the white space is also counted as a child node object
Rootelement.nodeiterator ();//Get Multiple Node objects
2)rootelement.element ("label name");//Gets the first child label object
Rootelement.elements ();//Get All Child label objects
Rootelement.elementiterator ();//Gets all the child label objects by means of iterators
4. Get the Tag Property object
Element element = rootelement.element ("label name");
Attribute Attribute = Element.attribute (the first few attributes/"attribute names");
Attribute.getname ();//Gets the key of the property
Attribute.getvalue ();//Gets the value of the property
Element.attributes (); Get all Property objects
Element.attributeiterator (); Gets all the property objects by means of iterators
Element.attributevalue (the first few attributes/"attribute names"); Get the value of the Property object directly
5. Get the text inside the tag
list<element> list = Element.elements ();
for (Element ele:list) {
String text = Ele.gettext ();//Get text content
System.out.println (text);
}
To write the XML file in code step:
Importing dom4j jar Packages
Through the documentation Help class create Docdocument doc = Documenthelper.createdocument ();//Add a root tag element rootele = doc.addelement ("Students ");//Add a sub-label element ele = rootele.addelement (" student ");//Add an attribute Ele.addattribute (" id "," 9527 ") to the student tag;// Add a name and school number label and content to student element element1 = ele.addelement ("sname"); Element1.addtext ("Zhang San"); Element Element2 = ele.addelement ("Sid"), Element2.addtext ("007"),//writes the In-memory doc to the hard disk outputformat FORMAT1 = Outputformat.createcompactformat (); OutputFormat format2 = Outputformat.createprettyprint (); Beautiful format//in the development phase to facilitate our debugging can use beautiful format//project developed well after the line, we want to adjust to compact format to reduce the volume of the XML file XMLWriter writer = new XMLWriter (New FileOutputStream ( "Mydoc.xml"), FORMAT2); Writer.write (doc); Writer.close ();
To modify the XML file steps in code:
Find the node you want to modify
1) Modify the value of the property
Attribute.setvalue ("new attribute value");
2) Modify the text
Element.settext ("new text");
3) Delete tags, properties
Element.detach ();
Attribute.detach ();
Write back to hard disk overwrite the original file
XMLWriter XMLWriter = new XMLWriter (New FileOutputStream ("XML file name. xml"));
Xmlwriter.write (DOC);
Xmlwriter.close ();
Attached:XPath: Mainly for the node object required for fast XML acquisition
1. Import dom4j jar Package and Support XPath technology jar package
2. Use the XPath method:
list<node> list = Rootelement.selectnodes ("XPath expression"); Querying multiple Node objects
Node Nade = Rootelement.selectsinglenode ("XPath expression"); Querying a Node object
3. XPath expression
/absolute Path Select the tag under the tag
Relative paths all tags under the label, representing selection elements that are not divided into any hierarchy.
* Wildcard means matching all elements
[] condition indicates which element is selected under what conditions
The @ property represents the Select Attribute node, which locates a property name
and relationships (equivalent to &&) of conditions
Not () Take counter
Text () literal indicates selection of text content
egg:string Path = "//user[@id = ' 2 ']/name"; Select the name tag under the user tag for all user tags in the id= "2"
Third, sax parsing
Sax parsing has two parts, parsers and event handlers
The parser is the XmlReader interface, responsible for reading the XML document, and sending events to the event handler (also the event source)
Event handler ContentHandler interface, responsible for responding to events sent and processing XML documents
To simplify development, the ContentHandler class DefaultHandler class is provided.
Event handlers
public class mycontenthandler extends defaulthandler{//called at the beginning of document parsing, this method will only be called once @overridepublic void startdocument () throws saxexception {super.startdocument ();} Call @overridepublic void startelement at the beginning of the label (string uri, string localname, string qname, attributes attributes) throws saxexception { //uri:xml document namespace (not available) //localname: The name of the tag (not available) //qname: The name of the label with the namespace (use this with the label name) // Attributes: The attribute set of the label Super.startelement (uri, localname, qname, attributes);} When parsing the contents of a tag, call @overridepublic void characters (char[] ch, int start, int Length) throws saxexception {//ch: The byte array that is currently read to the Textnode (text node)//start: Byte start position, 0 reads all// Length: Current Textnode super.characters (ch, start, length);} Call @overridepublic void endelement at the end of the label (string uri, string localname, string qname) throws saXexception {super.endelement (uri, localname, qname);} Called after the document resolves, the method will only call once @overridepublic void enddocument () throws saxexception { Super.enddocument ();}}
Parser
Gets the parser factory object, which gets the parser object
SAXParserFactory factory = Saxparserfactory.newinstance ();
SAXParser parser = Factory.newsaxparser ();
Resolves the specified file using the specified DefaultHandler
Parser.parse (New file ("filename. xml"), New Mycontenthandler ());
Iv. Pull Analysis
Xmlpull is more concise than sax and does not need to scan through the entire stream
Steps:
Import Kxml2-2.3.0.jar Xmlpull_1_1_3_4c.jar third-party jar packages
Gets the parser factory object, which gets the parser object xmlpullparserfactory factory = xmlpullparserfactory.newinstance (); Xmlpullparser parser = factory.newpullparser ();//Associate XML file Parser.setinput (new FileInputStream ("filename. xml"), "Utf-8");//Get Event Type Int type = parser.geteventtype ();// Xmlpullparser.start_document; document start event 0//xmlpullparser.end_document; Document End Event 1//xmlpullparser.start_tag; Start tag event 2//xmlpullparser.end_tag; end tag event 3//xmlpullparser.text; represents the text 4while (type != xmlpullparser.end_document) {string tagname = parser.getname ();switch (type) { Case xmlpullparser.start_tag:if ("label name". Equals (TagName)) {//action (JavaBean)}else if ("tag name". Equals (TagName)) {//...} Break;case xmlpullparser.end_tag:if ("label name". Equals (TagName)) {//operation (add)}break;} Let the pointer jump to the next line, re-assign the type, or it will die Loop Type = parser.next ()}//serialize the in-memory data to the hard disk to permanently save//Get the Parser factory object, thereby obtaining the serializer xmlpullparserfactory factory = xmlpullparserfactory.newinstance (); XmlserializEr serializer = factory.newserializer ();//Set Output stream association XML file Serializer.setoutput (new FileOutputStream ("file name. xml"), "Utf-8");//write document declaration (document start) serializer.startdocument ("Utf-8", true);// Parameter two: document is independent//write start root tag serializer.starttag (null, "Students");//Parameter 1 namespace general to null parameter 2 tag name// Write the child label of the root tag student start tag Serializer.starttag (null, "student"); Serializer.attribute (null, "id", "id value" );//write the attribute//write the child label of the student label name start tag Serializer.starttag (null, "name");//write the text serializer.text ("text content") in the name tag; /write name end tag Serializer.endtag (null, "name");//The same method writes student tag's sub-label age//writes student end tag Serializer.endtag (NULL, "student");//The same method of writing multiple student, generally using the loop//write end root tag serializer.endtag (null, "Students");// Document End Serializer.enddocument ();
XML Extensible Markup Language