从资产文件中解析XML

时间:2013-11-24 14:13:09

标签: java android xml android-parser android-pullparser

我在资产文件夹中有一个我需要解析的xml文件。 这是xml的结构:

<quran>
    <sura index="1" name="الفاتحة">
        <aya index="1" text="In the name of Allah, Most Gracious, Most Merciful."/>
        <aya index="2" text="Praise be to Allah, the Cherisher and Sustainer of the worlds;"/>
        <aya index="3" text="Most Gracious, Most Merciful;"/>
        <aya index="4" text="Master of the Day of Judgment."/>
        <aya index="5" text="Thee do we worship, and Thine aid we seek."/>
        <aya index="6" text="Show us the straight way,"/>
        <aya index="7" text="The way of those on whom Thou hast bestowed Thy Grace, those whose (portion) is not wrath, and who go not astray."/>
    </sura>
    <sura index="2" name="البقرة">
        <aya index="1" text="A. L. M."/>
        <aya index="2" text="This is the Book; in it is guidance sure, without doubt, to those who fear Allah;"/>
        <aya index="3" text="Who believe in the Unseen, are steadfast in prayer, and spend out of what We have provided for them;"/>
        <aya index="4" text="And who believe in the Revelation sent to thee, and sent before thy time, and (in their hearts) have the assurance of the Hereafter."/>
        <aya index="5" text="They are on (true) guidance, from their Lord, and it is these who will prosper."/>
        <aya index="6" text="As to those who reject Faith, it is the same to them whether thou warn them or do not warn them; they will not believe."/>
        <aya index="7" text="Allah hath set a seal on their hearts and on their hearing, and on their eyes is a veil; great is the penalty they (incur)."/>
        <aya index="8" text="Of the people there are some who say: &quot;We believe in Allah and the Last Day;&quot; but they do not (really) believe."/>
        <aya index="9" text="Fain would they deceive Allah and those who believe, but they only deceive themselves, and realise (it) not!"/>
    </sura>
</quran>

有人可以帮助我并告诉我如何解析这个xml以获取每个sura元素的索引和名称以及每个aya元素的索引和文本。

现在我只有这段代码:

XmlPullParserFactory pullParserFactory;
        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

                InputStream in_s = getApplicationContext().getAssets().open("filename.xml");
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in_s, null);
        } catch (XmlPullParserException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

由于

2 个答案:

答案 0 :(得分:2)

XPath xpath = XPathFactory.newInstance().newXPath();  
String expression = "//sura";  
InputSource inputSource = new InputSource("filename.xml");  
NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);  

for(int i=0; i<nodes.getLength(); i++) {  
    Node sura = nodes.item(i);  
    NamedNodeMap attributes = sura.getAttributes();  
    attributes.getNamedItem("name").getNodeValue()  
}  

答案 1 :(得分:1)

InputSource构造函数需要一个相对路径。因此,如果它位于assets文件夹中,那么您必须输入“/assets/filename.xml”

相关问题