英文:
how to get data from xml file with the (ID) number in android studio
问题
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class XMLParser {
public static void main(String[] args) {
try {
File inputFile = new File("your_xml_file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
int targetChapterID = 2; // Replace with the desired ChapterID
NodeList chapterList = doc.getElementsByTagName("Chapter");
for (int i = 0; i < chapterList.getLength(); i++) {
Element chapter = (Element) chapterList.item(i);
int chapterID = Integer.parseInt(chapter.getAttribute("ChapterID"));
if (chapterID == targetChapterID) {
NodeList verseList = chapter.getElementsByTagName("Verse");
for (int j = 0; j < verseList.getLength(); j++) {
Element verse = (Element) verseList.item(j);
String text = verse.getTextContent();
System.out.println("Text from Verse " + (j + 1) + ": " + text);
}
break; // Exit loop after finding the desired chapter
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: This code assumes that you have a properly formatted XML file named "your_xml_file.xml" in the same directory as the Java file. Make sure to replace the value of targetChapterID
with the ChapterID you want to retrieve the text for.
英文:
<Chapter ChapterID="1" ChapterName="The Opening">
<Verse VerseID="1"><![CDATA[text1 ]]></Verse>
<Verse VerseID="2"><![CDATA[text2 ]]></Verse>
<Verse VerseID="3"><![CDATA[text3 ]]></Verse>
<Verse VerseID="4"><![CDATA[text4 ]]></Verse>
<Verse VerseID="5"><![CDATA[text5 ]]></Verse>
<Verse VerseID="6"><![CDATA[text6 ]]></Verse>
<Verse VerseID="7"><![CDATA[text7 ]]></Verse>
</Chapter>
<Chapter ChapterID="2" ChapterName="The main">
<Verse VerseID="1"><![CDATA[text1 ]]></Verse>
<Verse VerseID="2"><![CDATA[text2 ]]></Verse>
<Verse VerseID="3"><![CDATA[text3 ]]></Verse>
<Verse VerseID="4"><![CDATA[text4 ]]></Verse>
<Verse VerseID="5"><![CDATA[text5 ]]></Verse>
<Verse VerseID="6"><![CDATA[text6 ]]></Verse>
<Verse VerseID="7"><![CDATA[text7 ]]></Verse>
</Chapter>
<Chapter ChapterID="3" ChapterName="The ending">
<Verse VerseID="1"><![CDATA[text1 ]]></Verse>
<Verse VerseID="2"><![CDATA[text2 ]]></Verse>
<Verse VerseID="3"><![CDATA[text3 ]]></Verse>
<Verse VerseID="4"><![CDATA[text4 ]]></Verse>
<Verse VerseID="5"><![CDATA[text5 ]]></Verse>
<Verse VerseID="6"><![CDATA[text6 ]]></Verse>
<Verse VerseID="7"><![CDATA[text7 ]]></Verse>
</Chapter>
so that was the XML file > As you can see, each Chapter contains words and an id for it
what i want to do it to get all the text from a chapter by its id in java
for example some thing like :
get Chapter.ChapterID = (int)
and then get all the text from that Chapter with that id
I know I am bad at asking the question or idea I want
but...
I hope the idea arrived
答案1
得分: 0
我建议您查看不同的XML解析库。存在许多不同且有帮助的教程(例如https://examples.javacodegeeks.com/java-xml-parser-tutorial)。
如果您对某些解释不理解或者在编码过程中遇到问题,您可以提一个新的问题。
还有一件事,在在stackoverflow提问之前,请尝试先搜索解决方案。您可能遇到的许多问题在过去已经在stackoverflow或类似的论坛/网站上得到了解答。
英文:
I would suggest that you take a look at different XML Parsing Libaries. There exists a lot of different, helpful tutorials (for example https://examples.javacodegeeks.com/java-xml-parser-tutorial).<br>
If you don't understand some of the explanation or if you get problems while coding, you can ask a new question. <br>
One more thing, try to search for a solution first, before asking at stackoverflow. A lot of questions which you might have, have been already been answered in the past on stackoverflow or on similar forums/websites.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论