简体   繁体   English

从资源解析XML文件

[英]Parsing XML file from resources

I am attempting to parse an XML file from Resources. 我正在尝试从参考资料解析XML文件。 After I have the file parsed I intend to perform some XPATH functions on it. 解析完文件后,我打算对其执行一些XPATH功能。

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. 调试时,代码在parse语句中引发IOException错误。 The logcat shows the error as logcat显示错误为

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

Any clues as to what I am doing wrong... 关于我在做什么错的任何线索...

You are attempting to pass a full resource name from Resources.getResourceName to DocumentBuilder.parse which expects a URI string. 您试图将完整的资源名称从Resources.getResourceName传递给需要URI字符串的DocumentBuilder.parse These are not the same thing. 这些不是一回事。

This is why you get a MalformedURLException 这就是为什么您得到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. 调用DocumentBuilder.parse的变体,该变体采用InputStream并改用res.openRawResource 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. 使用Resources.getXml返回XML提取解析器,并使用它而不是使用DOM API来解析XML。

Try 尝试

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

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM