想求一个 关于xml 的dome (包括数据的封装 以及 xml的解析)
刚看完了一些教程,但是感觉还是不够 特得想大家找个DOME 看看。
没有DOME的说下思路也可以 具体的怎么实现,大家给麻烦下啦.
来着有分!!!!!!!!!!!!
------解决方案--------------------清单 1. 示例 XML 文档(catalog.xml)
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML Catalog-->
<?target instruction?>
<journal title="XML Zone"
publisher="IBM developerWorks">
<article level="Intermediate" date="December-2001">
<title>Java configuration with XML Schema</title>
<author>
<firstname>Marcello</firstname>
<lastname>Vitaletti</lastname>
</author>
</article>
</journal>
</catalog>
然后使用同一个解析器修改 catalog.xml,清单 2 是修改后的 XML 文档,catalog-modified.xml。
清单 2. 修改后的 XML 文档(catalog-modified.xml)
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML catalog-->
<?target instruction?>
<journal title="XML Zone"
publisher="IBM developerWorks">
<article level="Introductory" date="October-2002">
<title>Create flexible and extensible XML schemas</title>
<author>
<firstname>Ayesha</firstname>
<lastname>Malik</lastname>
</author>
</article>
</journal>
</catalog>
清单 3 所示的例子程序 XmlDom4J.java 用于创建 XML 文档 catalog.xml。
清单 3. 生成 XML 文档 catalog.xml 的程序(XmlDom4J.java)
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
public class XmlDom4J{
public void generateDocument(){
Document document = DocumentHelper.createDocument();
Element catalogElement = document.addElement("catalog");
catalogElement.addComment("An XML Catalog");
catalogElement.addProcessingInstruction("target","text");
Element journalElement = catalogElement.addElement("journal");
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
Element articleElement=journalElement.addElement("article");
articleElement.addAttribute("level", "Intermediate");
articleElement.addAttribute("date", "December-2001");
Element titleElement=articleElement.addElement("title");
titleElement.setText("Java configuration with XML Schema");
Element authorElement=articleElement.addElement("author");
Element firstNameElement=authorElement.addElement("firstname");
firstNameElement.setText("Marcello");
Element lastNameElement=authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
document.addDocType("catalog",
null,"file://c:/Dtds/catalog.dtd");
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File("c:/catalog/catalog.xml") ));
output.write( document );
output.close();
}
catch(
IOException e){System.out.println(e.getMessage());}
}
public static void main(String[] argv){
XmlDom4J dom4j=new XmlDom4J();
dom4j.generateDocument();
}}