美好的一天,我正在尝试解析位于我的资产文件夹中的 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;
}
我不知道缺少什么,因为它在尝试解析它时会中断。我猜它的模式,但不知道如何去做。提前致谢