Using DOM4J and using Element.selectsinglenode Error resolution

Source: Internet
Author: User
Tags processing instruction xpath ibm developerworks
Element root = Document.getrootelement ();

List recordenvlist = document.selectnodes ("//gml:envelope"); Code that will go wrong
Element el= (Element) Root.selectsinglenode ("info[@id = '" +id+ "']");//or This code
The error message is: Noclassdeffounderror:org/jaxen ... org.dom4j.DocumentFactory.createXPath reason: dom4j XPath API relies on Jaxen to complete Workaround: Add Jaxen.jar to the system. Jaxen.jar Download Path: http://jaxen.codehaus.org/releases.html

---------------------------------------------------------Reference-ibm:http://www.ibm.com/developerworks/cn/xml/ X-dom4j.html---------------------------------------------------------------------

The dom4j API contains a tool for parsing XML documents. In this article, you will use this parser to create a sample XML document. Listing 1 shows this sample XML document, Catalog.xml. Listing 1. Sample XML document (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>

Then using the same parser to modify the Catalog.xml, listing 2 is the modified XML document, Catalog-modified.xml. Listing 2. Modified XML document (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>

The advantage of using the parser included with DOM4J is that DOM4J has local XPath support, compared to the user's DOM API. The DOM parser does not support the use of XPath to select nodes.

This article includes the following sections: Pre-set document modification document creation

Pre-set

This parser can be obtained from http://dom4j.org. The settings enable de>dom4j-1.4/dom4j-full.jarde> to be accessed in Classpath, which includes the Dom4j class, the XPath engine, and the SAX and DOM interfaces. If you have used the SAX and DOM interfaces contained in the JAXP parser, add de>dom4j-1.4/dom4j.jarde> to the classpath. De>dom4j.jar de> includes the Dom4j class and the XPath engine, but does not contain SAX and DOM interfaces.




Create a document

This section discusses the process of creating an XML document using the DOM4J API and creating a sample XML document, Catalog.xml.

To import the Dom4j API class using the import statement:

Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;

Create a document instance using the De>documenthelperde> class. De>documenthelperde> is the dom4j API factory class that generates XML document nodes.

Document document = Documenthelper.createdocument ();

Use the De>addelement () De> method to create the root element de>catalogde>. The de> addelement () de> is used to add elements to an XML document.

Element catalogelement = document.addelement ("catalog");

Add the comment "an XML catalog" using the De>addcomment () De> method in the de>catalogde> element.

Catalogelement.addcomment ("An XML catalog");

Use the De>addprocessinginstruction () De> method in the De>catalogde> element to add a processing instruction.

Catalogelement.addprocessinginstruction ("Target", "text");

Use the De>addelement () De> method in the De>catalogde> element to increase the de>journalde> element.

Element journalelement =  catalogelement.addelement ("Journal");

Use the De>addattribute () De> method to add de>titlede> and De>publisherde> properties to the de>journalde> element.

Journalelement.addattribute ("title", "XML Zone");
         Journalelement.addattribute ("publisher", "IBM developerWorks");

Adds a de>journalde> element to the de>articlede> element.

Element articleelement=journalelement.addelement ("article");

Adds de>levelde> and De>datede> properties to the de>articlede> element.

Articleelement.addattribute ("level", "intermediate");
      Articleelement.addattribute ("date", "December-2001");

Adds the de>titlede> element to the de>articlede> element.

Element titleelement=articleelement.addelement ("title");

Use the De>settext () De> method to set the text of the de>articlede> element.

Titleelement.settext ("Java configuration with XML Schema");

Adds the de>authorde> element to the de>articlede> element.

Element authorelement=articleelement.addelement ("author");

Adds the de>firstnamede> element to the de>authorde> element and sets the text of the element.

Element  firstnameelement=authorelement.addelement ("FirstName");
     Firstnameelement.settext ("Marcello");

Adds the de>lastnamede> element to the de>authorde> element and sets the text of the element.

Element lastnameelement=authorelement.addelement ("LastName");
     Lastnameelement.settext ("Vitaletti");

You can use the De>adddoctype () De> method to add a document type description.

Document.adddoctype ("Catalog", NULL, "FILE://C:/DTDS/CATALOG.DTD");

This adds the document type description to the XML document:

<! DOCTYPE Catalog SYSTEM "FILE://C:/DTDS/CATALOG.DTD" >

If the document is to be validated using document type definition (DTD) documentation, you must have Doctype.

XML declarations De><?xml version= "1.0" encoding= "UTF-8"?>de> are automatically added to the XML document.

The example program shown in Listing 3 Xmldom4j.java is used to create an XML document Catalog.xml. Listing 3. Program to generate XML document 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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.