Android用saxparser解析xml
作者:互联网
我试图在Android上解析带有SaxParser的xml文件.
这是我的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car model="CitroenC3">
<maintenances>
<xm:maintenance xmlns:xm="it.a.b.android.c.car.m" distance="" price="">
<xm:type></xm:type>
</xm:maintenance>
</maintenances>
<chargings>
<xc:charging xmlns:xc="it.a.b.c.fuelconsumption.car.m" quantity="18" price="20" distance="400" consumption="14"/>
</chargings>
</car>
</cars>
这是代码:
// Handling XML
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlResourceParser parser = getResources().getXml(R.xml.data);
// Create handler to handle XML Tags ( extends DefaultHandler )
DataSaxHandler myXMLHandler = new DataSaxHandler();
xr.setContentHandler(myXMLHandler);
InputStream is= getResources().openRawResource(R.xml.data);
xr.parse(new InputSource(is));
在xr.parse后我有异常:
03-22 15:24:04.248:INFO / System.out(415):XML Pasing Excpetion =
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: not well-formed (invalid token)
可能有什么不对?
非常感谢.
解决方法:
AFAIR,res /文件夹下的任何xml文件在放入.apk之前都会被编译.
尝试将XML文件移动到assets /文件夹并从那里加载它:
xr.parse(new InputSource(getAssets().open("data.xml")));
标签:android,xml,parsing,expat-parser,saxparser 来源: https://codeday.me/bug/20190827/1736666.html