简体   繁体   English

如何从资产解析XML?

[英]How to parse XML from assets?

Suppose I have an XML file in my assets with such data 假设我的资产中有一个包含此类数据的XML文件

<mods day="30" month="8" year="2012">
        <mod id="1" name="1a">Hello123</mod>
        <mod id="2" name="2a">Hello123</mod>
        <mod id="3" name="3a">Hello123</mod>
    </mods>

<mods day="31" month="8" year="2012">
        <mod id="1" name="1a">Hello123</mod>
        <mod id="2" name="2a">Hello123</mod>
        <mod id="3" name="3a">Hello123</mod>
    </mods>

and I need to find a certain "mod" parameter by inputting day, month, year and id. 并且我需要通过输入日期,月份,年份和ID查找特定的“ mod”参数。

here's how far I got. 这是我走了多远。

c.setTime(new Date());

String y = sdy.format(c.getTime());
String m = sdm.format(c.getTime());
String d = sdd.format(c.getTime());


InputStream istr = this.getAssets().open("mods.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
factory.setNamespaceAware(true); 
XmlPullParser xrp;
xrp = factory.newPullParser();
xrp.setInput(istr, "UTF-8");  

// not sure what to do next...

If I got it right, now I need to somehow query this xrp with my needed parameters, right? 如果我做对了,现在我需要以某种方式用所需的参数查询此xrp,对吗?

Thanks! 谢谢!

Putting the XML as an XML resource in res/xml/ , not in assets/ , it will parse 10x faster, courtesy of the optimizations in place for XML resources. 将XML作为XML资源放置在res/xml/ ,而不是在assets/ ,由于对XML资源进行了优化,因此解析速度将提高10倍。 Hence, I recommend using XML resources instead of XML in assets. 因此,我建议使用XML资源而不是资产中的XML。 You can see this CommonsWare sample project he uses the XMLPullParser to parse the xml resource. 您可以看到这个CommonsWare 示例项目,他使用XMLPullParser来解析xml资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM