Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

java - Parse xml nodes having text with any namespace using jsoup

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...