当前位置: 移动技术网 > IT编程>开发语言>Java > XML Dom4j和Pull 官网中简单介绍

XML Dom4j和Pull 官网中简单介绍

2018年09月05日  | 移动技术网IT编程  | 我要评论

            dom4j

 

parsing xml

  one of the first things you'll probably want to do is to parse an xml document of some kind. this is easy to do in <dom4j>. the following code demonstrates how to this.

解析:

  首先你可能会想要去解析各种xml文件,在dom4j中这是很容易的。以下代码演示了这么解析。

import java.net.url;

import org.dom4j.document;

import org.dom4j.documentexception;

import org.dom4j.io.saxreader;

public class foo {

    public document parse(url url) throws documentexception {

        saxreader reader = new saxreader();

        document document = reader.read(url);

        return document;

    }

}

 

using iterators

  a document can be navigated using a variety of methods that return standard java iterators. for example

使用迭代器

  使用不同的方式来操作文件以获得java的标准迭代器,例如:

public void bar(document document) throws documentexception {

    element root = document.getrootelement();

    // iterate through child elements of root

    for (iterator<element> it = root.elementiterator(); it.hasnext();) {

        element element = it.next();

        // do something

    }

    // iterate through child elements of root with element name "foo"

    for (iterator<element> it = root.elementiterator("foo"); it.hasnext();) {

        element foo = it.next();

        // do something

    }

    // iterate through attributes of root

    for (iterator<attribute> it = root.attributeiterator(); it.hasnext();) {

        attribute attribute = it.next();

        // do something

    }

 }

 

 

powerful navigation with xpath

  in <dom4j> xpath expressions can be evaluated on the document or on any node in the tree (such as attribute, element orprocessinginstruction). this allows complex navigation throughout the document with a single line of code. for example

 

xpath强大的导航能力

  dom4j 中的xpath表达式可以在文档或者树的任何节点(例如属性,元素或者进程指令)上求值,它使用一行代码在整个文档中进行复杂的导航,例如:

public void bar(document document) {

    list<node> list = document.selectnodes("//foo/bar");

    node node = document.selectsinglenode("//foo/bar/author");

    string name = node.valueof("@name");

}
 

 

 

for example if you wish to find all the hypertext links in an xhtml document the following code would do the trick.

举个例子,如果你想要找到xhtml文档中所有的超文本链接,可以根据以下的代码。

 

public void findlinks(document document) throws documentexception { 

    list<node> list = document.selectnodes("//a/@href");

    for (iterator<node> iter = list.iterator(); iter.hasnext();) {

        attribute attribute = (attribute) iter.next();

        string url = attribute.getvalue();

    }

}

 

 

if you need any help learning the xpath language we highly recommend the  which allows you to learn by example.

如果您需要帮助学习xpath语言,我们强烈推荐zvon教程,它允许您通过示例学习

 

fast looping

  if you ever have to walk a large xml document tree then for performance we recommend you use the fast looping method which avoids the cost of creating an iterator object for each loop. for example

快速循环

  如果您必须遍历大型xml文档树,那么为了提高性能,我们建议您使用快速循环方法,这样可以避免为每个循环创建迭代器对象的成本。例如

public void treewalk(document document) {

    treewalk(document.getrootelement());

}

public void treewalk(element element) {

    for (int i = 0, size = element.nodecount(); i < size; i++) {

        node node = element.node(i);

        if (node instanceof element) {

            treewalk((element) node);

        }

        else {

            // do something…

        }

    }

}

 

creating a new xml document

  often in <dom4j> you will need to create a new document from scratch. here's an example of doing that.

新建一个xml文档

  在dom4j中你将会经常从头开始创建一个新的文档,下面就是新建文档的例子。

import org.dom4j.document;

import org.dom4j.documenthelper;

import org.dom4j.element;

public class foo {

     public document createdocument() {

        document document = documenthelper.createdocument();

        element root = document.addelement("root");

         element author1 = root.addelement("author")

            .addattribute("name", "james")

            .addattribute("location", "uk")

            .addtext("james strachan");

 

        element author2 = root.addelement("author")

            .addattribute("name", "bob")

            .addattribute("location", "us")

            .addtext("bob mcwhirter");

 

        return document;

    }

}

 

 

 

writing a document to a file

  a quick and easy way to write a document (or any node) to a writer is via the write() method.

把文档写到文件中

  向写入器写入文档(或任何节点)的快速简便方法是通过write()方法。

filewriter out = new filewriter("foo.xml");

document.write(out);

out.close();

 

 

  if you want to be able to change the format of the output, such as pretty printing or a compact format, or you want to be able to work with writer objects or outputstream objects as the destination, then you can use the xmlwriter class.

  如果希望能够更改输出的格式,比如漂亮的打印或紧凑格式,或者希望能够使用writer对象或outputstream对象作为目标,那么可以使用xmlwriter类。

 

import org.dom4j.document;

import org.dom4j.io.outputformat;

import org.dom4j.io.xmlwriter;

 

public class foo {

    public void write(document document) throws ioexception {

        // lets write to a file

        try (filewriter filewiter = new filewriter("output.xml")) {

            xmlwriter writer = new xmlwriter(filewriter);

            writer.write( document );

            writer.close();

        }

 

        // pretty print the document to system.out

        outputformat format = outputformat.createprettyprint();

        writer = new xmlwriter(system.out, format);

        writer.write( document );

 

        // compact format to system.out

        format = outputformat.createcompactformat();

        writer = new xmlwriter(system.out, format);

        writer.write(document);

        writer.close();

    }

}

 

 

converting to and from strings

  if you have a reference to a document or any other node such as an attribute or element, you can turn it into the default xml text via the asxml() method.

字符串的转换

  如果您有对文档或任何其他节点(如属性或元素)的引用,您可以通过asxml()方法将其转换为默认的xml文本。

 

 

document document = …;

string text = document.asxml();

 

  if you have some xml as a string you can parse it back into a document again using the helper method documenthelper.parsetext()

  如果有一些xml作为字符串,可以使用辅助方法documenthelper.parsetext()将其解析回文档

 

string text = "<person> <name>james</name> </person>";

document document = documenthelper.parsetext(text);

 

transforming a document with xslt

  applying xslt on a document is quite straightforward using the  api from oracle. this allows you to work against any xslt engine such as xalan or . here is an example of using jaxp to create a transformer and then applying it to a document.

 

使用xslt转换文档

  使用oraclejaxp api对文档应用xslt非常简单。这允许您对抗任何xslt引擎,比如xalansaxon。下面是一个使用jaxp创建转换器并将其应用到文档的示例。

 

 
import javax.xml.transform.transformer;

import javax.xml.transform.transformerfactory;

import org.dom4j.document;

import org.dom4j.io.documentresult;

import org.dom4j.io.documentsource;

 

public class foo {

 

   public document styledocument(document document, string stylesheet) throws exception {

        // load the transformer using jaxp

        transformerfactory factory = transformerfactory.newinstance();

        transformer transformer = factory.newtransformer(new streamsource(stylesheet));

 

        // now lets style the given document

        documentsource source = new documentsource(document);

        documentresult result = new documentresult();

        transformer.transform(source, result);

 

        // return the transformed document

        document transformeddoc = result.getdocument();

        return transformeddoc;

    }

}


 

 

 

 


    xmlpull

主要的特点:


  1、simple interface 接口单一

     2、implementation independent实现独立

  3、ease of use 操作简单    

    •   start document
    •   start_tag
    •   text
    •   end_tag
    •   end_document

  4、versatility 多功能性

  5、performance  性能较好

  6、minimal requirements 需求小(设备的要求,内存等)


code step-by-step


  首先我们要创建一个解析器实例,这个要求有以下三个步骤:


    1、获得xmlpull工厂实例


    2、(可选步骤)默认情况下的工厂模式将生成没有名称空间的解析器;要更改setnamespaceaware()函数,必须要被调用


    3、创建一个解析器的实例


以下代码为实现方式:


xmlpullparserfactory factory=xmlpullparserfactory.newinstance();

factory.setnamespaceaware(true);

xmlpullparser xpp=factory.newpullparser();

下一步是设置解析器的输入:


xpp.setinput(new filereader(args [i]));

 


  接下来就可以开始进行解析了。


    为了检索下一个事件,典型的xmlpull应用将会反复地调用next()函数,直到end_document事件,进程才会被停止。


public void processdocument(xmlpullparser xpp)throws exception{
  int eventtype=xpp.gettype();
  do{
    if(eventtype==xpp.start_document){
    system.out.println(“start document”);
    }else if(eventtype==xpp.end_document){
      system.out.println(“end documen!”);
    }else if(eventtype == xpp.start_tag) {
      processstartelement(xpp);
    } else if(eventtype == xpp.end_tag) {
         processendelement(xpp);
    } else if(eventtype == xpp.text) {
      processtext(xpp);
    }
    eventtype = xpp.next();
  } while (eventtype != xpp.end_document);

}
}

 


  让我们看看如何处理start标记,与处理结束标签是非常相似的-主要的区别是结束标签没有属性。


 public void processstartelement (xmlpullparser xpp)

    {

        string name = xpp.getname();

        string uri = xpp.getnamespace();

        if ("".equals (uri)) {

            system.out.println("start element: " + name);

        } else {

            system.out.println("start element: {" + uri + "}" + name);

        }

    }

 

  现在让我们看看如何检索和打印元素内容:


public void processtext (xmlpullparser xpp) throws xmlpullparserexception

    {

        char ch[] = xpp.gettextcharacters();

        int start = xpp.gettextcharactersstart();

        int length = xpp.gettextcharacterslength();

        system.out.print("characters:    \"");

        for (int i = start; i < start + length; i++) {

            switch (ch[i]) {

                case '\\':

                    system.out.print("\\\\");

                    break;

                case '"':

                    system.out.print("\\\"");

                    break;

                case '\n':

                    system.out.print("\\n");

                    break;

                case '\r':

                    system.out.print("\\r");

                    break;

                case '\t':

                    system.out.print("\\t");

                    break;

                default:

                    system.out.print(ch[i]);

                    break;

            }

        }

        system.out.print("\"\n");

    }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网