1

我需要解析的资产文件夹中有一个 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();
        }

谢谢

4

2 回答 2

3
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()  
}  
于 2013-11-24T14:30:20.390 回答
1

InputSource 构造函数需要一个相对路径。所以如果它在资产文件夹中,那么你必须把“/assets/filename.xml”

于 2013-11-24T20:30:42.783 回答