Study Notes - dom4j and sax

 SAX articles :

What is SAX : SAX, the full name of Simple API for XML, is both an interface and a software package. It is an alternative to XML parsing. In layman's terms, it is an interface used to parse xml files.

SAX Features : SAX is an event-driven "push" model for processing XML . The SAX parser does not build a complete document tree like the DOM, but activates a series of events when the document is read, which are pushed to the event Handlers, and event handlers then provide access to the document content. It scans the document line by line, parsing it as it scans. Since the application only checks the data as it is read, there is no need to store the data in memory, which is a huge advantage for parsing large documents .

Use of SAX :

public class SaxDemo {
        public static void main(String[] args) throws ParserConfigurationException, SAXException, Exception {
// TODO auto-generated method stub
// 1. Get its sax parser factory
    SAXParserFactory factory =SAXParserFactory. newInstance();
    / /2. Get the parser
    SAXParser parser=factory.newSAXParser();
       //3. Get the reader
    XMLReader reader = parser.getXMLReader();
       //4. Set the event handler for the reader, as can be seen from here sax is XML event driven for the model
    reader.setContentHandler(new DefaultHandler() {


@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO auto-generated method stub
/*ch - from XML The character of the document
start - the starting position in the
array length - the number of characters read from the array */ 
super.characters(ch, start, length);
System.out.println(new String(ch,start,length));
}


@Override
public void startDocument() throws SAXException {
// TODO auto-generated method stub
super.startDocument ();
System.out.println("document start");
}


@Override
public void endDocument() throws SAXException {
// TODO auto-generated method stub
super.endDocument();
System.out.println("docunment end" );
}


@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
// TODO auto-generated method stub
super.startElement(uri, localName, qName, attributes);
//qName is the element the qualified name, or empty if the qualified name is not available
System.out.println("<"+qName+">");
}


@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO 自动生成的方法存根
super.endElement(uri, localName, qName);
System.out.println("</"+qName+">");}    });       //5.读取文件    reader.parse("WebContent/WEB-INF/web.xml");}


   
   







}

Note: the functionality of sax is limited to reading files unlike dom4j below

DOM4j _

Definition of DOM4j :  DOM4J is an open source XML parsing package produced by dom4j.org. DOM4J applies to the Java platform, uses the Java Collections framework and fully supports DOM, SAX and JAXP.

Function : According to my current learning experience, the function is to read and write xml files, and use SAX technology to read when reading.

Several core functions of DOM4J will be implemented through code:

First of all, all functions need to read the xml document, so the xml document can be encapsulated as a class and called directly.

Encapsulate document creation class

package dom4j.demo;

/*每次都要用,所有封装起来直接调用*/
public class Dom4jCreateDocument {
public static Document getDocument(String uri) throws DocumentException {//参数为uri,这是xml文件的地址
            SAXReader reader=new SAXReader();
            Document document =reader.read(new File(uri));
return document;
}

}

其次,增,删,改操作都需要回写xml文件。则将xml文件也封装起来,以供使用:

封装回写类

public class Dom4jwrite {

public static void write(Document document,String uri) throws Exception, FileNotFoundException
{
//4.回写
/* XMLWriter   writer =new XMLWriter(new OutputStreamWriter(new FileOutputStream("WebContent/WEB-INF/web.xml"), "utf-8"));*/
     /*这种方法可以格式化xml且解决字符乱码问题*/
org.dom4j.io.OutputFormat format=org.dom4j.io.OutputFormat.createPrettyPrint();
     format.setEncoding("utf-8");
    XMLWriter writer =new XMLWriter(new FileOutputStream(uri),format);
  writer.write(document);
      writer.close();
}
}

查操作:

public class Dom4jRead {


public static void main(String[] args) throws DocumentException {
//1.得到decoument对象
Document document =Dom4jCreateDocument.getDocument("WebContent/WEB-INF/web.xml");
        //读取xml文件的信息
//2.读取根元素
Element root =document.getRootElement();


//获取根下的第一个子元素
Element element_1 =root.element("book");
//获取根下的第二个子元素
Element element_2 =root.element("movie");
//获取book下的第一个子元素
Element element_1_1=element_1.element("name");
//获取movie下的第二个子元素
Element element_2_2=element_2.element("auther");
 
//通过迭代语句获取element所有book节点中的所有字节点
List<Element> elements= element_1.elements();
Iterator<Element> itr=element_1.elementIterator();
while(itr.hasNext())
{
 
Element list =itr.next();
          System.out.println(list.getText());
}
 
//3.获取元素下的文本信息
String text1=element_1_1.getText();
String text2=element_2_2.getText();
System.out.println(text1);
System.out.println(text2);
//获取属性的语句
String id=element_1.attributeValue("id");
System.out.println(id);
}

}

增操作:

package dom4j.demo
public class Dom4jAdd {
public static void main(String[] args) throws DocumentException, Exception {
// TODO 自动生成的方法存根
     //1.得到document
Document document =Dom4jCreateDocument.getDocument("WebContent/WEB-INF/web.xml");
//2.得到要添加到某个元素里面的位置
Element element =document.getRootElement().element("book");

//3.创建子元素
    Element date=DocumentHelper.createElement("date");
    //增加内容
    date.addText("2018/5/2");
    //增加属性
    date.addAttribute("ch", "man");
    //element.add(date);(这个无法指定插入的位置)
//创建子元素并且可以选择插入位置   
    List<Element> list=element.elements();
    list.add(1,date);

//4.回写
      Dom4jwrite.write(document,"WebContent/WEB-INF/web.xml");

}

}

tips增操作有两种方法,第一种通过获取要增加的位置的父节点,然后将要增加的内容设置后好通过add方法存到父节点中,只能放到这个父节点的最后位置

第二种通过获得父节点所有的子节点并存入list集合中,使用list的add(index,name)方法,将其存到指定索引位置。

改操作:

package dom4j.demo;
public class Dom4jUpdate {


public static void main(String[] args) throws FileNotFoundException, Exception {
// TODO 自动生成的方法存根
//获取document
Document document =Dom4jCreateDocument.getDocument("WebContent/WEB-INF/web.xml");
//获取根节点
Element  root= document.getRootElement();
//获取date元素
Element element=root.element("book").element("date");
//更新它的数值
element.setText("18/05/02");

//更改属性操作
//获取要更改属性的元素
Element element1=root.element("book");
element1.addAttribute("id", "2");//这里不是使用set方法而是add方法。

//调用回写函数
Dom4jwrite.write(document, "WebContent/WEB-INF/web.xml");
}
}

tips改方法很简单,基本就是重新设置需要更改的内容

删操作:

public class Dom4jRemove {

public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
    //1.获取document
        Document document =Dom4jCreateDocument.getDocument("WebContent/WEB-INF/web.xml");
/*(1)。删除某个节点必须获得他的父节点才能删除,不能自己删除自己*/
Element element =document.getRootElement().element("book").element("date");
//得到父节点并删除它
element.getParent().remove(element);
/*(2)。删除元素的属性操作*/
element.remove(element.attribute("ch"));
    //调用回写函数
Dom4jwrite.write(document, "WebContent/WEB-INF/web.xml");

}

}

tips删除某个节点,必须获得他的父节点取删除它,因为自己并不能删除自己

如果想要往xml文件写内容,使用add方法将会很麻烦,幸运的是可以通过字符串来写入xml文件

package dom4j.demo;

public class Dom4jToString {

public static void main(String[] args) throws Exception {

String str=" <movie><name>杨瑞琦</name><auther>余飞</auther></movie> ";//要存入的内容

//得到document
Document document =Dom4jCreateDocument.getDocument("WebContent/WEB-INF/web.xml");
//获得根节点
Element element=document.getRootElement();
//将字符串转换为xml文件
Document doc=DocumentHelper.parseText(str);
//因为xml文件还有自己的标准结构文件,所以转换后的文件还有标准结构文件
//解决方法:获取这个xml文件的头节点,就是我们需要添加的内容

element.add(doc.getRootElement());
//调用回写函数
Dom4jwrite.write(document, "WebContent/WEB-INF/web.xml");
}

}

tipsxml文件建立好时,会自带标准接口文件


如图,此时获取这个新建的xml文件的头节点,这时候头节点不包括这个标准结构文件。将其存入要存入的xml文件即可

贴下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<books> 
  <book id="2"> 
    <name>红楼梦</name>  
    <date>18/05/02</date>  
    <auther>曹雪芹</auther>  
    <price>23</price> 
  </book>  
  <movie> 
    <name>杨瑞琪</name>  
    <auther>余飞</auther> 
  </movie> 

</books>


Guess you like

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