想求一个 关于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();
}}
清单 4 所示的示例程序 Dom4JParser.java 用于把 catalog.xml 文档修改成 catalog-modified.xml 文档。
清单 4. 用于修改 catalog.xml 的程序(Dom4Jparser.java)
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.List;
import java.util.Iterator;
import org.dom4j.io.XMLWriter;
import java.io.*;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Dom4JParser{
public void modifyDocument(File inputXml){
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
List list = document.selectNodes("//article/@level" );
Iterator iter=list.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
if(attribute.getValue().equals("Intermediate"))
attribute.setValue("Introductory");
}
list = document.selectNodes("//article/@date" );
iter=list.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
if(attribute.getValue().equals("December-2001"))
attribute.setValue("October-2002");
}
list = document.selectNodes("//article" );
iter=list.iterator();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("title");
while(iterator.hasNext()){
Element titleElement=(Element)iterator.next();
if(titleElement.getText().equals("Java configuration with XML
Schema"))
titleElement.setText("Create flexible and extensible XML schema");
}
}
list = document.selectNodes("//article/author" );
iter=list.iterator();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("firstname");
while(iterator.hasNext()){
Element firstNameElement=(Element)iterator.next();
if(firstNameElement.getText().equals("Marcello"))
firstNameElement.setText("Ayesha");
}
}
list = document.selectNodes("//article/author" );
iter=list.iterator();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("lastname");
while(iterator.hasNext()){
Element lastNameElement=(Element)iterator.next();
if(lastNameElement.getText().equals("Vitaletti"))
lastNameElement.setText("Malik");
}
}
XMLWriter output = new XMLWriter(
new FileWriter( new File("c:/catalog/catalog-modified.xml") ));
output.write( document );
output.close();
}
catch(DocumentException e)
{
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] argv){
Dom4JParser dom4jParser=new Dom4JParser();
dom4jParser.modifyDocument(new File("c:/catalog/catalog.xml"));
}
}
[解决办法]
这是我以前回答别人问题的程序,(http://topic.csdn.net/u/20080611/08/cb0894cc-e3dd-4600-95d9-147af5ac616d.html)
也是解析XML文档的,希望对你有用
[javacode]
我给你一个自己用LOG4J做的
package dom4j;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class testCSDN {
public static String DOM_FILE_PATH = new String("testCSDN.xml");
public static void main(String[] args) throws DocumentException {
new testCSDN().parseXML(DOM_FILE_PATH);
}
public void parseXML(String DOM_FILE_PATH) throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read(DOM_FILE_PATH);
Element root = document.getRootElement();
Element foo = null;
Element elm = root.element("resourceitem");
Iterator <Element> it = root.elementIterator("resourceitem");
while (it.hasNext()) {
StringBuffer temp = new StringBuffer();
foo = it.next();
temp.append(foo.elementText("title") + " ");
temp.append(foo.elementText("keywords") + " ");
temp.append(foo.elementText("kind") + " ");
temp.append(foo.elementText("describe") + " ");
temp.append(foo.elementText("date") + " ");
temp.append(foo.elementText("url") + " ");
temp.append(foo.elementText("author") + " ");
temp.append(foo.elementText("publisher"));
System.out.println(temp.toString());
}
}
}
运行结果:
07各地高考作文题 作文,高考 doc 07各地高考作文题 2007-12-1 8:50:43 http://192.168.0.122:8080/Query/source/07各地高考作文题.doc 李明 山东师大
汽车动画 汽车,动画 wmv 学生作品 2007-12-1 8:50:43 http://192.168.0.122:8080/Query/source/jc.wmv 王朋 山东师大
[解决办法]
是要解析和写xml的例子吗?
[解决办法]
我写好一个XML例子,用DOM4J解析时出现一个错误:The type org.jaxen.VariableContext cannot be resolved. It is indirectly referenced from required .class files
代码:XMLTableModel model=new XMLTableModel(definition,document);
错误是在这个代码上出现的,哪位高手麻烦给解释下
[解决办法]
网上有很多例子,而且都是写好的不用调试就可以了
到google上去找吧,比你在这里等来的快
[解决办法]
http://www.java2000.net/f295
有几个例子 自己看下