如何在Android Studio中通过(ID)号从XML文件中获取数据。

huangapple go评论86阅读模式
英文:

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.

英文:
 &lt;Chapter ChapterID=&quot;1&quot; ChapterName=&quot;The Opening&quot;&gt;
&lt;Verse VerseID=&quot;1&quot;&gt;&lt;![CDATA[text1 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;2&quot;&gt;&lt;![CDATA[text2 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;3&quot;&gt;&lt;![CDATA[text3 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;4&quot;&gt;&lt;![CDATA[text4 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;5&quot;&gt;&lt;![CDATA[text5 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;6&quot;&gt;&lt;![CDATA[text6 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;7&quot;&gt;&lt;![CDATA[text7 ]]&gt;&lt;/Verse&gt;
&lt;/Chapter&gt;
&lt;Chapter ChapterID=&quot;2&quot; ChapterName=&quot;The main&quot;&gt;
&lt;Verse VerseID=&quot;1&quot;&gt;&lt;![CDATA[text1 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;2&quot;&gt;&lt;![CDATA[text2 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;3&quot;&gt;&lt;![CDATA[text3 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;4&quot;&gt;&lt;![CDATA[text4 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;5&quot;&gt;&lt;![CDATA[text5 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;6&quot;&gt;&lt;![CDATA[text6 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;7&quot;&gt;&lt;![CDATA[text7 ]]&gt;&lt;/Verse&gt;
&lt;/Chapter&gt;
&lt;Chapter ChapterID=&quot;3&quot; ChapterName=&quot;The ending&quot;&gt;
&lt;Verse VerseID=&quot;1&quot;&gt;&lt;![CDATA[text1 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;2&quot;&gt;&lt;![CDATA[text2 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;3&quot;&gt;&lt;![CDATA[text3 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;4&quot;&gt;&lt;![CDATA[text4 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;5&quot;&gt;&lt;![CDATA[text5 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;6&quot;&gt;&lt;![CDATA[text6 ]]&gt;&lt;/Verse&gt;
&lt;Verse VerseID=&quot;7&quot;&gt;&lt;![CDATA[text7 ]]&gt;&lt;/Verse&gt;
&lt;/Chapter&gt;

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).&lt;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.

huangapple
  • 本文由 发表于 2020年7月28日 22:03:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63135952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定