最好的java Xml解析器来操作/编辑现有的xml文档 [英] best java Xml parser to manipulate/edit an existing xml document

查看:94
本文介绍了最好的java Xml解析器来操作/编辑现有的xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:我有一个使用xml命名空间和xml架构的现有xml文档(UTF-8)。我需要解析一个特定的元素,将内容(也需要使用xml名称空间前缀)附加到此元素,然后再次写出Document。

TASK : I have an existing xml document (UTF-8) which uses xml namespaces and xml schema. I need to parse to a particular element, append content (that also needs to use xml namespace prefixes) to this element and then write out the Document again.

这是我应该用于此TASK的最佳XML解析器库吗?

which is the best XML parser library that I should be using for this TASK ?

我见过一个上一个帖子(最适合Java的XML解析器)但不确定dom4j或JDOM对于namespaces / xmlSchema是否有用,并且对UTF-8字符有良好的支持。

I've seen a previous thread (Best XML parser for Java) but was not sure if dom4j or JDOM is any good for namespaces/xmlSchema and good support for UTF-8 characters.

一些解析器似乎是一个任务。
JDom

Dom4J

XOM

WoodStock

Some parsers that seems like a task for
JDom
Dom4J
XOM
WoodStock

知道哪一个是最好的? :-)我使用JDK 6并且不希望使用内置的SAX / DOM工具来完成这项工作,因为这需要我编写太多的代码。

Any idea which one is the best ? :-) I use JDK 6 and would prefer NOT to use the built-in SAX/DOM facilities to do this job because that requires me to write too much code.

有助于有一些做这样的任务的例子。

Would help to have some examples of doing such a task.

推荐答案

使用JDOM,获取一个InputStream并使其成为一个Document:

Using JDOM, taking an InputStream and making it a Document:

InputStream inputStream = (InputStream)httpURLConnection.getContent();
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
DocumentBuilder docbuilder = docbf.newDocumentBuilder();
Document document = docbuilder.parse(inputStream, baseUrl);

此时,您在Java对象中拥有XML。完成。简单。

At that point, you have the XML in a Java object. Done. Easy.

你可以使用文档对象和Java API来浏览它,或者也可以使用XPath,我觉得它更容易(一旦我学会了它)。

You can either use the document object and the Java API to just walk through it, or also use XPath, which I find easier (once I learned it).

构建一个XPath对象,需要一点点:

Build an XPath object, which takes a bit:

public static XPath buildXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new AtomNamespaceContext());
    return xpath;
}


public class AtomNamespaceContext implements NamespaceContext {

    public String getNamespaceURI(String prefix) {
        if (prefix == null)
            throw new NullPointerException("Null prefix");
        else if ("a".equals(prefix))
            return "http://www.w3.org/2005/Atom";
        else if ("app".equals(prefix))
            return "http://www.w3.org/2007/app";
        else if ("os".equals(prefix))
            return "http://a9.com/-/spec/opensearch/1.1/";
        else if ("x".equals(prefix)) 
            return "http://www.w3.org/1999/xhtml";
        else if ("xml".equals(prefix))
            return XMLConstants.XML_NS_URI;
        return XMLConstants.NULL_NS_URI;
    }

    // This method isn't necessary for XPath processing.
    public String getPrefix(String uri) {
        throw new UnsupportedOperationException();
    }

    // This method isn't necessary for XPath processing either.
    public Iterator getPrefixes(String uri) {
        throw new UnsupportedOperationException();
    }
}

然后只需使用它,(谢天谢地)没有'花费很多时间:

Then just use it, which (thankfully) doesn't take much time at all:

return Integer.parseInt(xpath.evaluate("/a:feed/os:totalResults/text()", document));

这篇关于最好的java Xml解析器来操作/编辑现有的xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆