使用jsoup解析具有任何命名空间的文本的xml节点 [英] Parse xml nodes having text with any namespace using jsoup

查看:174
本文介绍了使用jsoup解析具有任何命名空间的文本的xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用网址解析XML Jsoup

I am trying to parse XML from URL using Jsoup.

在这个给定的XML中,有一些带有命名空间的节点。

In this given XML there are nodes with namespace.

ex:< wsdl:types>

现在我想将包含文本的所有节点都作为类型获取,但可以拥有任何命名空间。

Now I want to get all nodes which contain text as "types" but can have any namespace.

我能够使用表达式将此节点作为wsdl | types

I am able to get this nodes using expression as "wsdl|types".

但是如何将包含文本的所有节点作为具有任何命名空间的类型。 ?

But how can I get all nodes containing text as "types" having any namespace. ?

我尝试使用表达式* | types但是它没有用。

I tried with expression as "*|types" but it didn't worked.

请帮忙。

推荐答案

还没有这样的选择器。但是你可以使用一种解决方法 - 不像选择器那样容易阅读,但它是一种解决方案。

There is no such selector (yet). But you can use a workaround - a not as easy to read like a selector, but it's a solution.

/*
 * Connect to the url and parse the document; a XML Parser is used
 * instead of the default one (html)
 */
final String url = "http://www.consultacpf.com/webservices/producao/cdc.asmx?wsdl";
Document doc = Jsoup.connect(url).parser(Parser.xmlParser()).get();


// Elements of any tag, but with 'types' are stored here
Elements withTypes = new Elements();

// Select all elements
for( Element element : doc.select("*") )
{
    // Split the tag by ':'
    final String s[] = element.tagName().split(":");

    /*
     * If there's a namespace (otherwise s.length == 1) use the 2nd
     * part and check if the element has 'types'
     */
    if( s.length > 1 && s[1].equals("types") == true )   
    {
        // Add this element to the found elements list
        withTypes.add(element);
    }
}

您可以将此代码的基本部分放入方法,所以你得到这样的东西:

You can put the essential parts of this code into a method, so you get something like this:

Elements nsSelect(Document doc, String value)
{
    // Code from above
}

...

Elements withTypes = nsSelect(doc, "types");

这篇关于使用jsoup解析具有任何命名空间的文本的xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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