1

アセット フォルダにある xml を解析しようとしていますが、MalformedURLException: protocol not found が引き続き発生します。ここに私の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

これを試してください..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 に答える