Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been looking for a straight solution to parse an xml from the web using String for a couple of days now. Most of the examples out there are on how to create a url. I am looking to parse this url and also access the page and recover this file. I did read the javadoc, it describes the exact opposite example as well.

What I have tried:

This is my code so far:
import java.io.Serializable;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ChannelParser extends Object implements Serializable {

    private static Document loadTestDocument(String url) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(new URL(url).openStream());
    }

    public void processNode(Node currentNode) throws XPathExpressionException {

        switch (currentNode.getNodeType()) {

            case Node.ELEMENT_NODE:
                Document doc = (Document) currentNode;

                // normalize text representation of text
                doc.getDocumentElement().normalize();
                XPathFactory factory = XPathFactory.newInstance();
                XPath xpath = factory.newXPath();
                javax.xml.xpath.XPathExpression expr
                        = xpath.compile("example path");
                Object result = expr.evaluate(doc, XPathConstants.NODESET);
                 System.out.println(nodes.item(i).getNodeValue());
                Integer s = 1;
                List<String> list = new ArrayList<>();
                NodeList nodes = (NodeList) result;
                //Update/renew the headline
                for (int i = 0; i < list.getLength(); i++) {
                    Node n = List.getRootElement(i);
                   System.out.println(n.getElementsByTagName("title").item(0).getTextContent());
                    list.add(n.getNodeName());
                    s++;
                }
                    break;
                }
        }

    public static void main(String[] args) throws Exception {
        Document doc = loadTestDocument("example url");
        System.out.println(doc);
    }

    private void processChildNodes(NodeList childNodes) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    public class myErrorHandler implements ErrorHandler {
        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }

        @Override
        public void error(SAXParseException e) throws SAXParseException {
            throw e;
        }
        @Override
        public void warning(SAXParseException err) throws SAXParseException {
            System.err.println("Warning: " + err.getMessage());
        }

    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900