In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of the SAX parsing process in XML, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Create a new Demo1 class:
Import java.io.File;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;/** * the first SAX program to read xml files * @ author APPle * * / public class Demo1 {public static void main (String [] args) throws Exception {/ / 1. Create a SAXParser object / / SAXParser parser = SAXParserFactory.newInstance (). NewSAXParser (); / / 2. Call parse method / * parameter 1: xml document * parameter 2: subclass of DefaultHandler, default base class, so you can't new. Parses the contents of the specified file to XML using the subclass * / parser.parse (new File (". / src/contact.xml"), new MyDefaultHandler ()); / / using the specified DefaultHandler. Be sure to customize the MyDefaultHandler class here and implement the business logic in it. If you don't understand, die first. Remember this pattern. }}
Then the MyDefaultHandler of custom inheritance DefaultHandler is given.
Import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler / * SAX processor (how to parse xml documents) * @ author APPle * * / public class MyDefaultHandler extends DefaultHandler {/ * call * / @ Override public void startDocument () throws SAXException {System.out.println ("MyDefaultHandler.startDocument ()") when starting a document } / * call * @ param qName when starting the tag: indicates the tag name of the start tag * @ param attributes: indicates the (attribute) [list] * / @ Override public void startElement (String uri, String localName, String qName) contained in the start tag. Attributes attributes) throws SAXException {System.out.println ("MyDefaultHandler.startElement ()-- >" + qName) } / * call * @ param qName when ending the tag: end the tag name of the tag * / @ Override public void endElement (String uri, String localName, String qName) throws SAXException {System.out.println ("MyDefaultHandler.endElement ()->" + qName) } / * call * @ param ch when reading the text content: indicates all the text content currently read * @ param start: indicates the starting position of the current text content * @ param length: indicates the length of the current text content * / @ Override public void characters (char [] ch) Int start, int length) throws SAXException {/ / get the current text content String content = new String (ch,start,length) System.out.println ("MyDefaultHandler.characters ()-- >" + content);} / * * call * / @ Override public void endDocument () throws SAXException {System.out.println ("MyDefaultHandler.endDocument ()");}}
Printout, more intuitive understanding of the sax parsing process.
Thank you for reading this article carefully. I hope the article "sample Analysis of SAX parsing process in XML" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.