2013-12-23 72 views
0

我正在尝试解析资源中的XML文件。解析完文件后,我打算在其上执行一些XPATH函数。从资源解析XML文件

我的代码是:

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

当调试代码解析的语句抛出IOException错误。该logcat的显示误差

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

任何线索,我在做什么错?

+0

这是我写的问题,一个错字 - 上面现在 –

回答

2

您正在尝试将完整的资源名称从Resources.getResourceName传递到DocumentBuilder.parse,它需要一个URI字符串。这些不是一回事。

这就是为什么你得到一个MalformedURLException的

有一些你可以做到这一点,包括不同的方式:

  1. 呼叫的DocumentBuilder.parse的变体,它需要一个InputStream和使用res.openRawResource来代替。这可能是更小的变化。
  2. 使用Resources.getXml找回XML拉解析器,并使用它来代替使用DOM API来解析XML。
-1

请尝试使用我的运行代码....

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

-1修正张贴的代码墙太运行一个! –

+0

直到我的代码抛出一个异常的代码是相同的 - 除了我的问题领域是从资源文件中获取XML。 –

0

尝试

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