In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of the sample analysis of SAX in XML. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1.SAX parsing
When parsing a XML document using DOM, you need to read the entire XML document, build the Document object of the entire DOM tree in memory, and then manipulate the XML document. In this case, if the XML document is particularly large, it will consume a lot of the computer's memory and, in severe cases, may cause a memory overflow.
SAX parsing allows you to process the document when it is read, without having to wait until the entire document has been loaded.
Develop a SAX parser by inheriting DefaultHandler
[note] SAX is mainly used to parse XML documents and cannot modify, delete or add elements.
1.1.SAX parsing mechanism
Sax is a push mechanism where you create a sax parser that tells you when it finds content in an xml document (pushing events to you, similar to event listeners in java swing). It is up to the programmer to decide what to do with these discoveries.
In sax-based programs, there are five most common sax events:
1.startDocument ()-> tells you that the parser finds the beginning of the document and tells you that the parser starts scanning the document
2.endDocument ()-> tells you that the parser found the end of the document
3.startElement ()-> tells you that the parser has found a starting tag, and this event tells you the name of the tag, all attribute names and values of the element
4.characters ()-> tells you that the parser finds some text and will get an array of characters, the offset of the array and a length offset. With these three variables, you can get the text found by the parser.
5.endElement ()-> tells you that the parser found a closing tag, and the event tells you the name of the element
1.2.SAX parsing instance
Still use the XML example used in DOM parsing, as follows:
Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student
[steps]:
1. Create a SAX parsing factory using SAXParserFactory
SAXParserFactory spf = SAXParserFactory.newInstance ()
two。 Get the parser object through the SAX parsing factory
SAXParser sp = spf.newSAXParser ()
3. Associate a parsing object with an event handler object
Sp.parse ("src/myClass.xml", new MyHandler ())
The MyHandler here needs to be defined by itself, and it inherits DefaultHandler, and then overrides the five sax event methods mentioned above in the MyHandler class, or you can only override what you need.
For example, the MyHandler I wrote now is as follows:
Class MyHandler extends DefaultHandler {/ * finds the beginning of the document, the function will only be called once * / @ Override public void startDocument () throws SAXException {System.out.println ("startDocument");} / * finds that the document ends, and the function will only be called once * / @ Override public void endDocument () throws SAXException {System.out.println ("endDocument") } / * finds that when an element in XML starts, it will be repeatedly called * / @ Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {System.out.println ("element name:" + qName) } / * finds that an element in XML ends and will be repeatedly called * / @ Override public void endElement (String uri, String localName, String qName) throws SAXException {} / * to find the text in the XML file Will be repeatedly called * / @ Override public void characters (char [] ch, int start, int length) throws SAXException {/ / display text content String text = new String (ch,start,length) If (! text.trim (). Equals (")) {System.out.println (text);}
The running results are as follows:
As you can see, this is a traversal of XML documents, and all sax can do is traverse.
So, if we have such a need now: to show only the names and ages of all the students, but not the students' introductions, how can it be achieved?
We can define two Boolean variables isName and isAge in the MyHandler class, identify whether it is a name element or an age element in the startElement method, and if so, get the corresponding text in the characters method, as follows:
1. Define two Boolean variables
Private boolean isName = false;private boolean isAge = false
two。 Add judgment to the startElement method
@ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {if (qName.equals (first name)) {this.isName = true;} else if (qName.equals ("age")) {this.isAge = true;}}
3. Determine whether to get the text or not according to the identifier in the characters method
@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {/ / display text content String text = new String (ch,start,length); if (! text.trim (). Equals (") & (isName | | isAge) {System.out.println (text);} isName = false; isAge = false;}
Finally, remember to reset the two Boolean variables to false.
The running results are as follows:
1.SAX parsing
When parsing a XML document using DOM, you need to read the entire XML document, build the Document object of the entire DOM tree in memory, and then manipulate the XML document. In this case, if the XML document is particularly large, it will consume a lot of the computer's memory and, in severe cases, may cause a memory overflow.
SAX parsing allows you to process the document when it is read, without having to wait until the entire document has been loaded.
Develop a SAX parser by inheriting DefaultHandler
[note] SAX is mainly used to parse XML documents and cannot modify, delete or add elements.
1.1.SAX parsing mechanism
Sax is a push mechanism where you create a sax parser that tells you when it finds content in an xml document (pushing events to you, similar to event listeners in java swing). It is up to the programmer to decide what to do with these discoveries.
In sax-based programs, there are five most common sax events:
1.startDocument ()-> tells you that the parser finds the beginning of the document and tells you that the parser starts scanning the document
2.endDocument ()-> tells you that the parser found the end of the document
3.startElement ()-> tells you that the parser has found a starting tag, and this event tells you the name of the tag, all attribute names and values of the element
4.characters ()-> tells you that the parser finds some text and will get an array of characters, the offset of the array and a length offset. With these three variables, you can get the text found by the parser.
5.endElement ()-> tells you that the parser found a closing tag, and the event tells you the name of the element
1.2.SAX parsing instance
Still use the XML example used in DOM parsing, as follows:
Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student
[steps]:
1. Create a SAX parsing factory using SAXParserFactory
SAXParserFactory spf = SAXParserFactory.newInstance ()
two。 Get the parser object through the SAX parsing factory
SAXParser sp = spf.newSAXParser ()
3. Associate a parsing object with an event handler object
Sp.parse ("src/myClass.xml", new MyHandler ())
The MyHandler here needs to be defined by itself, and it inherits DefaultHandler, and then overrides the five sax event methods mentioned above in the MyHandler class, or you can only override what you need.
For example, the MyHandler I wrote now is as follows:
Class MyHandler extends DefaultHandler {/ * finds the beginning of the document, the function will only be called once * / @ Override public void startDocument () throws SAXException {System.out.println ("startDocument");} / * finds that the document ends, and the function will only be called once * / @ Override public void endDocument () throws SAXException {System.out.println ("endDocument") } / * finds that when an element in XML starts, it will be repeatedly called * / @ Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {System.out.println ("element name:" + qName) } / * finds that an element in XML ends and will be repeatedly called * / @ Override public void endElement (String uri, String localName, String qName) throws SAXException {} / * to find the text in the XML file Will be repeatedly called * / @ Override public void characters (char [] ch, int start, int length) throws SAXException {/ / display text content String text = new String (ch,start,length) If (! text.trim (). Equals (")) {System.out.println (text);}
The running results are as follows:
As you can see, this is a traversal of XML documents, and all sax can do is traverse.
So, if we have such a need now: to show only the names and ages of all the students, but not the students' introductions, how can it be achieved?
We can define two Boolean variables isName and isAge in the MyHandler class, identify whether it is a name element or an age element in the startElement method, and if so, get the corresponding text in the characters method, as follows:
1. Define two Boolean variables
Private boolean isName = false;private boolean isAge = false
two。 Add judgment to the startElement method
@ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {if (qName.equals (first name)) {this.isName = true;} else if (qName.equals ("age")) {this.isAge = true;}}
3. Determine whether to get the text or not according to the identifier in the characters method
@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {/ / display text content String text = new String (ch,start,length); if (! text.trim (). Equals (") & (isName | | isAge) {System.out.println (text);} isName = false; isAge = false;}
Finally, remember to reset the two Boolean variables to false.
The running results are as follows:
Thank you for reading! This is the end of this article on "sample Analysis of SAX in XML". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.