1

美好的一天,我正在尝试解析位于我的资产文件夹中的 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;
    }

我不知道缺少什么,因为它在尝试解析它时会中断。我猜它的模式,但不知道如何去做。提前致谢

4

2 回答 2

0

以这种方式创建文档:

  Document doc = docBuilder.parse(new InputSource(new ByteArrayInputStream(xmlvalues.getBytes("utf-8"))));
于 2013-10-22T11:47:24.040 回答
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);
                    }
于 2013-10-22T11:43:52.557 回答