gpt4 book ai didi

android - 解析具有相同标签名称的问题嵌套标签

转载 作者:太空狗 更新时间:2023-10-29 14:18:01 25 4
gpt4 key购买 nike

我需要解析类似结构的xml,

<document>
<questions>
<question_id>1</question_id>
<cluster_id>1</cluster_id>
<question_name>question</question_name>
<question_hint>sample hint</question_hint>
<question_type>1</question_type>

<answers>
<option>answer</option>
<correct_answer>0</correct_answer>
<option>answer2</option>
<option>answer3</option>
<option>answer4</option>
</answers>
</questions>
<questions>
<question_id>2</question_id>
<cluster_id>1</cluster_id>
<question_name>Question 2</question_name>
<question_hint>sample hint</question_hint>
<question_type>1</question_type>

<answers>
<option>answer 1 for question 1</option>
<option>answer 2 for question 1</option>
<option>answer 2 for question 1</option>
<option>answer 2 for question 1</option>
<correct_answer>3</correct_answer>
</answers>
</questions></document>

我无法解析嵌套标签以获得类似问题名称:正确答案:选项一:选项二:选项三:选项四:我使用 sax 解析的值..但无法像上面的结构那样检索值..

最佳答案

static final String KEY_ITEM = "questions"; // parent node
static final String KEY_QUESTION = "question_id";
static final String KEY_QUESTIONNAME = "question_name";
static final String KEY_QUESTIONHINT = "question_hint";

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_ITEM);

// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
String name = parser.getValue(e, KEY_QUESTION); // name child value
String cost = parser.getValue(e, KEY_QUESTIONNAME); // cost child value
String description = parser.getValue(e, KEY_QUESTIONHINT); // description child value
}

关于android - 解析具有相同标签名称的问题嵌套标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19701776/

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