JAVA study notes _XML parsing and XPath expressions

1. DOM parsing: Parse xml files based on the DOM model. Load the xml file into memory at one time, and then build the Document tree in memory. It consumes more memory and is not suitable for reading large-capacity xml files.
1) The principle of DOM parsing: 
The xml parsing engine loads an xml file into the memory at one time, and creates a document object tree in the memory. Get or set xml file information through objects on the tree
2) Tools based on DOM parsing principle
dom4jtools
3) How to use dom4j (please refer to Baidu for specific code)
check:
Document doc= new SAXReader().read("xml文件");
Label:
element("name") query the first subtag
elements("name") Query all subtags of the specified name
elements() query all subtags
Attributes:
attributeValue("name") Get the attribute value according to the attribute name
attribute("name") Get the attribute object according to the attribute name
getName() property name / getValue() property value
   text:
getText() Get the text content of the current label
elementText("name") gets the sublabel text

increase:
Documentation: DocumentHelper.createDocument();
Label: addElement("name")   
属性:  addAttribute("name","value")
change:
Attribute value: setValue("Modify value") / addAttribute("Attribute with the same name","Modify value")
Text: setText("text")
delete:
Labels, properties: detach() / getParent().remove(label/property)
4) XPath
XPath expressions (emphasis) (see w3c for details)
/ Absolute path The slash is at the front, representing the root of the xml file. A slash in the middle indicates a child element.
// relative path select descendant elements (no hierarchy)
* wildcard selects all elements
[ ] Condition The element under which condition is selected. For example /AAA/BBB[1] selects the first BBB child element
@ attribute select attribute
= content(value)  
and logical and
text()   selects the text content
How XPath is used in dom4j
1) Import the xpath support jar package in the project. jaxen-1.1-beta-6.jar
2) Use the xpath method provided by dom4j
selectNodes(xpath expression): Query multiple node objects that meet the conditions 
selectSingleNode(xpath expression) Query a node object that meets the conditions






2.SAX parsing: Parse the xml file based on the principle of SAX. Advantage: The memory footprint is very small. Load a little, parse a little, process a little, then free the memory.
The official API of oracle-sun, in jdk, belongs to the specification of javase. org.xml.sax.* . There is no need to import the package.



Reading xml files based on SAX parsing
public class Demo1 {

public static void main(String[] args)throws Exception {
//1. Create a SAXParser object
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();

//2. Call the parse method to read and parse the xml file
/**
* Parameter 1: The specified file address
*/
File file = new File("./src/contact.xml");
/**
* Parameter two: Specify a subclass of DefaultHandler
*/
parser.parse(file, new MyDefaultHandler());
}


}


//event handler:
package gz.itcast.b_sax;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* event handler
* @author APPle
*/


public class MyDefaultHandler extends DefaultHandler {
/**
* Encounter the document start
*/
@Override
public void startDocument() throws SAXException {
System.out.println("The document starts to read");
}

/**
* encounter start tag
* @param qName: the currently encountered tag name
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("MyDefaultHandler.startElement()-->"+qName);
}

/**
* Encountered text content (including space wrapping)
* @param ch : Indicates all the text content of the entire xml document
* @param start: Indicates the starting position of the currently read text content
* @param length: Indicates the length of the currently read text content
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
//Get the text content currently read
String content = new String(ch,start,length);
System.out.println("MyDefaultHandler.characters()-->"+content);
}

/**
* encounter end tag
* @param qName: Indicates the name of the currently read end tag
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("MyDefaultHandler.endElement()-->"+qName);
}


/**
* When encountering an end document
*/
@Override
public void endDocument() throws SAXException {
System.out.println("The document has been read");
}

}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763996&siteId=291194637