How to get “prensentationURL” from XML Page with J

2019-08-26 18:24发布

问题:

I'm trying to get presentationURL from an XML page. I can get the location URL with:

    public String getLocation()
{
    return HTTPHeader.getValue(getData(), HTTP.LOCATION);
}

but it's not the exactly what I want. Url I get takes me to a document tree. What I want is to get "presentationURL" variable in this tree.

回答1:

Some like this. It is a fasest and simple method for your structure. You using XMLStreamReader to read xml by strings and check start\end elemens by name.

    StringBuffer xml = new StringBuffer(
            "<note>\n" +
                "<to>Tove</to>" +
                "<from>Jani</from>" +
                "<heading>" +
                    "<prensentationURL>maValue</prensentationURL>" +
                 "</heading>" +
                "<body>any any</body>" +
            "</note>");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new ByteArrayInputStream(xml.toString().getBytes()));
    String prensentationURL = null;
    while (streamReader.hasNext()) {
        streamReader.next();
        String name = null;
        if (streamReader.isStartElement()) {
            if ("prensentationURL".equals(streamReader.getLocalName())) {
                prensentationURL = streamReader.getElementText();
            }
        }
        if (streamReader.isEndElement() && streamReader.getLocalName().equals("prensentationURL")) break;
    }
    System.out.println(prensentationURL);


标签: java xml http