Parse xml nodes having text with any namespace using jsoup

自作多情 提交于 2020-01-28 11:30:26

问题


I am trying to parse XML from URL using Jsoup.

In this given XML there are nodes with namespace.

for ex: <wsdl:types>

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

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. ?

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

Please help.


回答1:


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


来源:https://stackoverflow.com/questions/23764992/parse-xml-nodes-having-text-with-any-namespace-using-jsoup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!