JAVAWEB Day 6 sax parsing xml

1. Parse xml using sax
- sax mode can not add or delete, only query operation
Print out the entire document and execute the parse method. The first parameter is the path to the xml, and the second parameter is the parser.With respect to parsers, you need to create a class, inherit the event resolution class, and override the methods in it.
1. Print the entire document now (note: do not wrap the output line, because the parsing will parse the space and the wrap line together)
java code

package sax;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Test {
    public static void main(String[] args) throws Exception {
        /**
         * 1.Create Parser Factory
         * 2.Create Parser
         * 3.Execute the parse method
         * 4.Write your own class, inherit the parser class, DefaultHandler
         * 5.Override the method
         */
        SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
        SAXParser saxParser=saxParserFactory.newSAXParser();
        saxParser.parse("src/p1.xml", new myDefaultHandler());
    }

}
class myDefaultHandler extends DefaultHandler{

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.print("<"+qName+">");
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        System.out.print(new String(ch,start,length));
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.print("</"+qName+">");
    }



}

xml file

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <p1>
      <name>zhangsan</name>
      <age>20</age>
   </p1>
   <p1>
      <name>lisi</name>
      <age>30</age>
   </p1>
</person>

2. Query all name element values
Define a member variable, flag=fasle, to determine if the start element is a name element, if it is a name element, set false to true, flag to true, print its contents in characters, and set the flag value to true when the end method is executed.

class myDefault2 extends DefaultHandler{
    boolean flag=false;
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //Determine if qName is name
        if("name".equals(qName)) {
            flag=true;
        }
    }   
    @Override
    //When flag is true, it resolves to the name element, printing out the value of the name element
    public void characters(char[] ch, int start, int length) throws SAXException {
        if(flag==true) {
            System.out.println(new String(ch,start,length));
        }
    }

    @Override
    //Set flag to false to indicate end of name
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if("name".equals(qName)) {
            flag=false;
        }
    }


}

3. Query the value of a specific name element
Set up an index to query by index

boolean flag=false;
int index=1;

public void characters(char[] ch, int start, int length) throws SAXException {
        if(flag==true && index==1) {
            System.out.println(new String(ch,start,length));
        }
    }

Keywords: xml Java encoding

Added by neogranas on Mon, 25 May 2020 19:59:42 +0300