DOM解析轻松入门(四)--DOM Level 3 Load and Save

 /*java and xml 3nd学习笔记
DOM解析轻松入门(四)--DOM Level 3 Load and Save
authors:shine
*/
DOM解析轻松入门(三)中大概讲解了DOM Levels,重点讲解了DOM Level 2 Modules中的Traversal和Range,这次我们就来看看DOM Level3:

Level 3:在Level2的基础上,增加了两个新的Module
a.DOM Level 3 Core (XML):提供了bootstrapping和InfoSet 机制(其中bookstrapping 后面有提,infoSet就是指在Document接口中加入了getXmlEncoding();getXmlStandalone();getXmlVersion();等控制XML Information的方法。)
b.DOM Level 3 Load & Save (LS):(顾名思义)
c.DOM Level 3 Validation (Validation):定义了根据DTD或Schema验证的接口。

由于一般的解析器只支持DOM Level 3 Core 和DOM Level 3 Load & Save (LS),所以Validation 就不浪费口水了。

DOM Level 3 Load & Save (LS):这个 Module是在org.w3c.dom.ls包中定义的。通过DOM Level3 Core中bootstrapping(自举机制)实现。

直接看例子:(这个例子实现了从test1.xml中解析内容,并写入到test2.xml文件中,其中做了schema验证)

1.先准备一个test1.xml

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:/workplace/test1.xsd">
 <name>San Guo</name>
 <description>dddddddddd!</description>
</book>

2.再准备一个test1.xsd (用xmlSpy生成)
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2007 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="name">
  <xs:simpleType>
   <xs:restriction base="xs:string">
    <xs:enumeration value="San Guo"/>
   </xs:restriction>
  </xs:simpleType>
 </xs:element>
 <xs:element name="description">
  <xs:simpleType>
   <xs:restriction base="xs:string">
    <xs:enumeration value="dddddddddd!"/>
   </xs:restriction>
  </xs:simpleType>
 </xs:element>
 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="name"/>
    <xs:element ref="description"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

3.开始解析
package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSSerializer;

public class LSTest {
 public static void main(String[] args) {
  try {
   //bootstrapping(自举机制)获得LS Module支持
   String xmlURI = "D://workplace//test1.xml";
   DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
   //获得DOM指定DOM Module的解析器parserLS
   DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
   LSParser parserLS = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
   
   //配置解析器parserLS(做schema验证,如果不做schema验证就不用配DOMConfiguration)
   DOMConfiguration config = parserLS.getDomConfig();
   config.setParameter("validate", Boolean.TRUE);
   config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
   config.setParameter("schema-location", "D://workplace//test1.xsd");
   
   //配置输入源lsInput
   LSInput lsInput = lsImpl.createLSInput();
   lsInput.setCharacterStream(new FileReader(new File(xmlURI)));
   Document doc = parserLS.parse(lsInput);
   
   LSTest test = new LSTest();
   test.save(lsImpl, doc, "D://workplace//test2.xml");
   
  } catch (ClassCastException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InstantiationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 //LSSerializer 接口定义把内存中的XML写入文件
 public void save(DOMImplementationLS lsImpl,Document doc,String xmlURI) {
  LSSerializer serializer = lsImpl.createLSSerializer();
  LSOutput lsOutput = lsImpl.createLSOutput();
  try {
   lsOutput.setCharacterStream(new FileWriter(new File(xmlURI)));
   serializer.write(doc, lsOutput);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  
 }
}

4.注意:
如果在test1.xml中随便加入一个<a/>(即:不符合schema的元素),再run一下,控制台输出:
[Error] :4:6: cvc-complex-type.2.4.a: Invalid content was found starting with element 'a'. One of '{description}' is expected.
呵呵,是不是比SAX中的ErrorHandler方便多了,但是值得注意的是,即便有[error],但是</a>还是会写入到test2.xml中,不信去看看结果。

感言:
通过了DOM解析轻松入门(一)(二)(三)(四)4篇文章,终于把DOM解析中的常用知识点搞定了(累),接下来就是JAXP

/*
JAXP基础入门(一)2008-2-22
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值