gpt4 book ai didi

java - 从 Assets 文件中解析 XML

转载 作者:行者123 更新时间:2023-11-29 05:33:51 25 4
gpt4 key购买 nike

我需要解析 Assets 文件夹中的 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();
}

谢谢

最佳答案

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()
}

关于java - 从 Assets 文件中解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20175520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com