Reprinted dom4j xml parsing

Parsing XML using dom4j
Use domj4 API to create and modify XML document level: primary
dom4j is an open source parsing XML documents XML framework. This article describes how to use the parser included in the dom4j to create and modify XML documents.
dom4j API XML document contains an analysis tool. This article will use the parser to create a sample XML document. Listing 1 shows the 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>

And then use the same parser modify catalog.xml, Listing 2 is the modified XML document, catalog-modified.xml.

Listing 2. The 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>

Compared with the W3C DOM API, use dom4j parser contained in the benefits of local ownership is dom4j XPath support. DOM parser does not support the use of XPath to select nodes.

This includes the following sections:

Modify the document to create a document preset preset

This parser can get from http://dom4j.org. By setting the classpath to dom4j-1.4/dom4j-full.jar able to access, the file includes dom4j class, XPath, and SAX and DOM interfaces engines. If you have used the JAXP SAX parser and DOM interfaces are included, adding to the classpath dom4j-1.4/dom4j.jar. dom4j.jar including the dom4j classes and XPath engine, but not including SAX and DOM interfaces.

Create a document

This section discusses the use of dom4j API to create XML documents in the process and create a sample XML document catalog.xml.

Use the import statement to import the dom4j API categories:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

Create a document using DocumentHelper class instance. DocumentHelper is to generate XML document node of the dom4j API factory class.

Document document = DocumentHelper.createDocument ();

Use the addElement () method to create the root element catalog. addElement () is used to add elements to the XML document.

Element catalogElement = document.addElement ("catalog");

Elements in the catalog using addComment () method to add a comment "An XML catalog".

catalogElement.addComment ("An XML catalog");

Elements in the catalog using addProcessingInstruction () method to add a processing instruction.

catalogElement.addProcessingInstruction ("target", "text");

Elements in the catalog using addElement () method to increase journal element.

Element journalElement = catalogElement.addElement ("journal");

Use addAttribute () method to add elements to the journal title and publisher attributes.

journalElement.addAttribute ("title", "XML Zone");
journalElement.addAttribute ("publisher", "IBM developerWorks");

Add the journal element to the article element.

Element articleElement = journalElement.addElement ("article");

Increase the level for the article and the date property element.

articleElement.addAttribute ("level", "Intermediate");
articleElement.addAttribute ("date", "December-2001");

Add elements to the article title element.

Element titleElement = articleElement.addElement ("title");

Use setText () method to set the text of article elements.

titleElement.setText ("Java configuration with XML Schema");

Elements in the article to add author element.

Element authorElement = articleElement.addElement ("author");

Adding firstname element in the author element and set the element's text.

Element firstNameElement = authorElement.addElement ("firstname");
firstNameElement.setText ("Marcello");

Increase in the author element and set the lastname element text element.

Element lastNameElement = authorElement.addElement ("lastname");
lastNameElement.setText ("Vitaletti");

You can use addDocType () method to add a document type description.

document.addDocType ("catalog", null, "file: / / c: / Dtds / catalog.dtd");

This increase in the document to the XML document type description:

<! DOCTYPE catalog SYSTEM "file: / / c: / Dtds / catalog.dtd">

If the document you want to use the document type definition (DTD) document verification, you must have Doctype.

XML declaration <? Xml version = "1.0" encoding = "UTF-8"?> Automatically added to the XML document.

Listing 3 shows examples of procedures used to create XML documents XmlDom4J.java catalog.xml.

Listing 3. Catalog.xml procedures generate XML documents (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 ();
))

This section discusses the process of creating XML documents, the next section will use the dom4j API changes introduced to create the XML document here.

Back to top

Modify the document

This section describes how to use the dom4j API modify the sample XML document catalog.xml.

Parsing XML documents using SAXReader catalog.xml:

SAXReader saxReader = new SAXReader ();
Document document = saxReader.read (inputXml);

SAXReader included in org.dom4j.io package.

inputXml from c: / catalog / catalog.xml create a java.io.File. Use XPath expressions to get from the article level element node list. If the level attribute value is "Intermediate" will be changed to "Introductory".

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");
)

Get a list of article elements, from the article's title element by element in an iterator, and modify the title element of the text.

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");
))

Elements through a similar process and modify the title author element.

Listing 4 shows an example program Dom4JParser.java catalog.xml document used to modify the document into the catalog-modified.xml.

Listing 4. Catalog.xml to modify the procedures (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"));
)
)

This section describes how to use the dom4j parser changes in the sample XML document. This parser does not use DTD or schema validation, XML. If you need to validate XML documents, can be explained using dom4j with the JAXP SAX parser.

分类:Java 时间:2010-04-29 人气:242
分享到:
blog comments powered by Disqus

相关文章

  • Java programming language commonly used in the method of updating XML documents 2010-09-28

    Quoted from http://www.ibm.com/developerworks/cn/java/l-javaxml/ This paper briefly discusses the Java programming language update XML documents in the four commonly used methods, and analysis of the merits of the four methods. Secondly, the article

  • XML documents using Libxml2 operation 2010-02-25

    1, Libxml2 Description: Libxml2 is an xml parser c-language version, was originally a tool for the Gnome project development, based on the MIT License is a free open source software. It addition to supporting the c-language version, it also supports

  • (Transfer) the use of PL / SQL DOM API parsing of XML documents 2010-04-17

    This program demonstrates the use of PL / SQL DOM API for parsing XML documents, - Use the SYS.XMLDOM and SYS.XMLPARSER package - Program Analysis of the input XML file, print the document for all elements and element attributes connect scott / tiger

  • Java parsing XML documents four methods 2010-05-07

    1) DOM (JAXP Crimson parser) DOM is platform-and language-independent manner, said the official W3C standard XML document. DOM is a hierarchical organization of nodes or pieces of information collection. This hierarchy allows the developer to find sp

  • Android to create XML documents 2010-08-10

    Now, we just look at a concrete example to illustrate how to use the XmlSerializer. private String writeXml () ( XmlSerializer serializer = Xml.newSerializer (); StringWriter writer = new StringWriter (); try ( serializer.setOutput (writer); / / <? X

  • Android parse XML documents using PULL 2010-12-14

    Android PULL technical analysis used in the system than using SAX XML documents much better technical performance. Android developers proposed technical parsing XML documents using PULL. SAX with similar, but the return type of event PULL technology

  • Java parsing XML documents (2): sax read xml file export excel 2010-03-22

    1. Concept SAX (Simple API for XML), event-based XML Simple API, parsing XML documents is one of the two API. 2. Sax parsing 1. Use to the package: javax.xml.parsers: parser factory tools org.xml.sax: provides the core SAX API For example: import jav

  • DOM4J on XML documents to read and write operations such as additions and deletions to change 2010-03-16

    XML documents with DOM4J on CRUD operations such as reading and writing is my own exercises, and notes there is no order and only later see use. Main method in the constructor to do the simple instructions, involved XML, XSD, DTD document is no longe

  • Used to create and modify XML documents DOM4J 2010-04-25

    Code: package com.willpower.parsexml; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentExcept

  • Using dom4j to create, edit XML documents and format the output and the Chinese to solve the problem 2010-08-22

    package com.holen.dom4j; import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.do

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.511 (s). 12 q(s)