How can I extract information from an XML that is stored in a String?

0

I'm consuming this tag of a and storing your content in a String p>

Its structure looks like this:

<opml version="1">
    <head>
        <title>Título</title>
        <status>200</status>
    </head>
    <body>
        <outline type="link" text="op_1" URL="http://dados.com/op.ashx?c=1" key="1"/>
        <outline type="link" text="op_2" URL="http://dados.com/op.ashx?c=2" key="2"/>
        <outline type="link" text="op_3" URL="http://dados.com/op.ashx?c=3" key="3"/>
        <outline type="link" text="op_4" URL="http://dados.com/op.ashx?c=4" key="4"/>
        <outline type="link" text="op_5" URL="http://dados.com/op.ashx?c=5" key="5"/>
        <outline type="link" text="op_6" URL="http://dados.com/op.ashx?c=6" key="6"/>
        <outline type="link" text="op_7" URL="http://dados.com/op.ashx?c=7" key="7"/>
    </body>
</opml> 

How can I do to read your type, text and URL of each outline?

    
asked by anonymous 20.03.2017 / 19:03

2 answers

1

You can do a regular expression to get just the data you need.

In the example above, with this input xml to capture the type and url you can do this:

         String xml =   "<opml version=\"1\">" +
                            "<head>" +
                                "<title>Título</title>" +
                                "<status>200</status>" +
                            "</head>" +
                            "<body>" +
                                "<outline type=\"link\" text=\"op_1\" URL=\"http://dados.com/op.ashx?c=1\" key=\"1\"/>" +
                                "<outline type=\"link\" text=\"op_2\" URL=\"http://dados.com/op.ashx?c=2\" key=\"2\"/>" +
                                "<outline type=\"link\" text=\"op_3\" URL=\"http://dados.com/op.ashx?c=3\" key=\"3\"/>" +
                                "<outline type=\"link\" text=\"op_4\" URL=\"http://dados.com/op.ashx?c=4\" key=\"4\"/>" +
                                "<outline type=\"link\" text=\"op_5\" URL=\"http://dados.com/op.ashx?c=5\" key=\"5\"/>" +
                                "<outline type=\"link\" text=\"op_6\" URL=\"http://dados.com/op.ashx?c=6\" key=\"6\"/>" +
                                "<outline type=\"link\" text=\"op_7\" URL=\"http://dados.com/op.ashx?c=7\" key=\"7\"/>" +
                            "</body>" +
                        "</opml> ";

        Pattern regex = Pattern.compile("((<(?i)outline).+?((?i)type=\"(.+?)\").+?((?i)url=\"(.+?)\").+?(\/>))");
        Matcher matcher = regex.matcher(xml);

        while (matcher.find()) {
            String type = matcher.group(4);
            String url = matcher.group(6);
            System.out.println("TYPE: " + type);
            System.out.println("URL: " + url);
            System.out.println();
        }

In this regular expression, each argument in parentheses means a grouping, so group 4 and group 6 are the ones you want to filter ( type and URL ), (?i) expression can be understood as ignoreCase

    
20.03.2017 / 19:46
0

I found another legal way to do it.

This way you can do the search by TAG and attribute.

I'll post if anyone prefers to do it this way.

private void le_o_xml2(String xml) throws ParserConfigurationException, IOException, SAXException {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));

    //LÊ TODAS AS TAG QUE SE CHAMAM "UOTLINE" . . .
    NodeList outline = doc.getElementsByTagName("outline");
    if (outline.getLength() > 0){
        for(int x = 0; x<outline.getLength(); x++) {
            //CONSOME OS ATRIBUTOS DA TAG . . .
            Element err = (Element) outline.item(x);
            System.out.println(err.getAttribute("type"));
            System.out.println(err.getAttribute("url"));
            System.out.println(err.getAttribute("text"));
            System.out.println(err.getAttribute("key"));
        }
    } else {
        // NÃO ENCONTROU NENHUMA TAG . . .
    }

}
21.03.2017 / 14:17