BENBUN Coder
BENBUN Coder

Reputation: 4881

Parsing XML file from resources

I am attempting to parse an XML file from Resources. After I have the file parsed I intend to perform some XPATH functions on it.

The code I have is :

Resources res = getResources();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
Document doc = null;
XPathExpression expr = null;

    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        doc = builder.parse(res.getResourceName(R.xml.cdatest01));
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  

When debugging the code throws an IOException error in the parse statement. The logcat shows the error as

java.net.MalformedURLException: unknown protocol: com.apps4care.mycarerecord

Any clues as to what I am doing wrong...

Upvotes: 0

Views: 3868

Answers (3)

Sỹ Phạm
Sỹ Phạm

Reputation: 540

Try

InputStream fIn = getResources().openRawResource(R.raw.hot);
Document doc=builder.parse(fIn);

Upvotes: 0

Steve Sowerby
Steve Sowerby

Reputation: 1158

You are attempting to pass a full resource name from Resources.getResourceName to DocumentBuilder.parse which expects a URI string. These are not the same thing.

This is why you get a MalformedURLException

There's a number of ways you could do this differently including:

  1. Call the variant of DocumentBuilder.parse which takes an InputStream and use res.openRawResource instead. That's probably the smaller change.
  2. Use Resources.getXml to get back an XML pull parser and use that instead of using the DOM API to parse the XML.

Upvotes: 2

nilesh patel
nilesh patel

Reputation: 832

Please try to use my running code....

    try {

        File fXmlFile = new File("/root/Desktop/staff.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        // optional, but recommended
        // read this -
        // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :"
                + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("Staff id : "
                        + eElement.getAttribute("id"));
                System.out.println("First Name : "
                        + eElement.getElementsByTagName("firstname")
                                .item(0).getTextContent());
                System.out.println("Last Name : "
                        + eElement.getElementsByTagName("lastname").item(0)
                                .getTextContent());
                System.out.println("Nick Name : "
                        + eElement.getElementsByTagName("nickname").item(0)
                                .getTextContent());
                System.out.println("Salary : "
                        + eElement.getElementsByTagName("salary").item(0)
                                .getTextContent());

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: -1

Related Questions