java.net.MalformedURLException:在资产文件夹中解析xml时找不到协议

时间:2013-10-22 11:24:22

标签: java android xml

美好的一天,我正在尝试解析位于我的资产文件夹中的xml,但我一直收到MalformedURLException:找不到协议。这是我的xml

<QCARConfig xsi:noNamespaceSchemaLocation="qcar_config.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Tracking>
        <ImageTarget name="stones" size="247 173"/>
        <ImageTarget name="chips" size="247 173"/>
        <ImageTarget name="tarmac" size="247 173"/>
    </Tracking>
</QCARConfig>

这是我的代码:

public class ParseXMLElement {
    private static final String TAG = "MY_TAG";
    Context mcontext;
    String xmlvalues;
    int xmlcount;

    ArrayList<String> targets = new ArrayList<String>();

    public ParseXMLElement(Context context) {
       this.mcontext = context;
        parseXML();
    }


    public String[] parseXML() {
        try {
            InputStream is = mcontext.getAssets().open("myVuforiaSample.xml");
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

             InputStreamReader reader = new InputStreamReader(is);
             StringBuilder builder = new StringBuilder();
             BufferedReader br = new BufferedReader(reader);
             String read = br.readLine();

             while(read != null){
                 builder.append(read);
                 read = br.readLine();
             }

             xmlvalues = builder.toString();
             Log.d(TAG, "xml values is " + xmlvalues);

             Document doc = docBuilder.parse(xmlvalues);

             NodeList list = doc.getElementsByTagName("Tracking");
              // use the list here
             xmlcount = list.getLength();

             Elements element = new Elements();
                 element.SetCount(xmlcount);

                 for(int i = 0; i < list.getLength(); i++){
                     Node node = list.item(i);
                     Element elt = (Element)node;

                     String name = elt.getElementsByTagName("ImageTarget").item(i).getTextContent().toString();
                     Log.d(TAG, name);
                     targets.add(name);

                 }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

        String[] myimageTargets = (String[]) targets.toArray(new String[targets.size()]);
        return myimageTargets;
    }

我不知道丢失了什么,因为它在尝试解析它时会中断。我猜它的架构,但不知道如何去做。提前致谢

2 个答案:

答案 0 :(得分:0)

for循环

中试试..
Node node = nodeList.item(i);
Element elt = (Element) node;
NodeList nameList1 = elt.getElementsByTagName("ImageTarget");
Element nameElement1 = (Element) nameList1.item(0);
String name = nameElement1.getAttribute("name");
targets.add(name);

也改变..

      InputStream raw = getApplicationContext().getAssets().open("myVuforiaSample.xml");            
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder= dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(raw);

            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("Tracking");

            Log.v("length--", ""+nodeList.getLength());

            for (int i = 0; i < nodeList.getLength(); i++) {
                          Node node = nodeList.item(i);
                          Element elt = (Element) node;
                          NodeList nameList1 = elt.getElementsByTagName("ImageTarget");
                          Element nameElement1 = (Element) nameList1.item(0);
                          String name = nameElement1.getAttribute("name");
                          targets.add(name);
                    }

答案 1 :(得分:0)

以这种方式创建文档:

  Document doc = docBuilder.parse(new InputSource(new ByteArrayInputStream(xmlvalues.getBytes("utf-8"))));