dom4j库的阅读、更新问题
这是我编写 xml 文件的代码:
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");
//pass this xml document
DTDGenerator dtd=new DTDGenerator(document,"catalog.dtd");
document.addDocType("catalog",
null,"catalog.dtd");
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File("catalog.xml") ));
output.write( document );
output.close();
}
catch(IOException e){System.out.println(e.getMessage());}
}
我的阅读代码是:
public static 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("catalog-modified.xml") ));
output.write( document );
output.close();
}
catch(DocumentException e)
{
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
我正在使用 dom4j 库,并且还借助 DTDGenerator 从我创建的 xml 生成 dtd 文件。写作部分一切正常。但阅读正在诅咒我。我相信我的 DTD 有问题..但我的输出 dtd 看起来相当令人印象深刻..但对阅读 xml 没有帮助:(.. 我的 Dtd 输出:
<!ELEMENT article ( title, author ) >
<!ATTLIST article date NMTOKEN #REQUIRED >
<!ATTLIST article level NMTOKEN #REQUIRED >
<!ELEMENT author ( firstname, lastname ) >
<!ELEMENT catalog ( journal ) >
<!ELEMENT firstname ( #PCDATA ) >
<!ELEMENT journal ( article ) >
<!ATTLIST journal publisher CDATA #REQUIRED >
<!ATTLIST journal title CDATA #REQUIRED >
<!ELEMENT lastname ( #PCDATA ) >
<!ELEMENT title ( #PCDATA ) >
我的 Xml 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
-<catalog>
<!--An XML Catalog-->
<?target text?>
-<journal title="XML Zone" publisher="IBM developerWorks">-<article date="December-2001" level="Intermediate"><title>Java configuration with XML Schema</title>-<author><firstname>Marcello</firstname><lastname>Vitaletti</lastname></author></article></journal></catalog>
错误是:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at com.xmlsample.XMLSampleRead.modifyDocument(XMLSampleRead.java:35)
at com.xmlsample.XMLSampleRead.main(XMLSampleRead.java:24)
Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
this is my code to write xml file:
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");
//pass this xml document
DTDGenerator dtd=new DTDGenerator(document,"catalog.dtd");
document.addDocType("catalog",
null,"catalog.dtd");
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File("catalog.xml") ));
output.write( document );
output.close();
}
catch(IOException e){System.out.println(e.getMessage());}
}
my reading code is:
public static 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("catalog-modified.xml") ));
output.write( document );
output.close();
}
catch(DocumentException e)
{
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
I am using dom4j library and also taken help of DTDGenerator to generate dtd file from my created xml. everything working fine at writing part..But reading is cursing me. I believe there is something wrong with my DTD..but my output dtd is quite impressive to look..But not helping in reading the xml:(..
My Dtd ouput:
<!ELEMENT article ( title, author ) >
<!ATTLIST article date NMTOKEN #REQUIRED >
<!ATTLIST article level NMTOKEN #REQUIRED >
<!ELEMENT author ( firstname, lastname ) >
<!ELEMENT catalog ( journal ) >
<!ELEMENT firstname ( #PCDATA ) >
<!ELEMENT journal ( article ) >
<!ATTLIST journal publisher CDATA #REQUIRED >
<!ATTLIST journal title CDATA #REQUIRED >
<!ELEMENT lastname ( #PCDATA ) >
<!ELEMENT title ( #PCDATA ) >
My Xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
-<catalog>
<!--An XML Catalog-->
<?target text?>
-<journal title="XML Zone" publisher="IBM developerWorks">-<article date="December-2001" level="Intermediate"><title>Java configuration with XML Schema</title>-<author><firstname>Marcello</firstname><lastname>Vitaletti</lastname></author></article></journal></catalog>
and the error is:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at com.xmlsample.XMLSampleRead.modifyDocument(XMLSampleRead.java:35)
at com.xmlsample.XMLSampleRead.main(XMLSampleRead.java:24)
Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与您的 DTD 无关。您需要将 jaxen 包含在您的类路径中。 Jaxen 是 dom4j 在计算 xpath 表达式时内部使用的 xpath 引擎。 Jaxen 是 dom4j 发行版的一部分。
This has nothing to do with your DTD. You need to include jaxen on your classpath. Jaxen is the xpath engine used internally by dom4j when evaluating xpath expressions. Jaxen is part of the dom4j distribution.