我需要解析具有类似结构的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来解析..但是无法像上面的结构那样检索值..
答案 0 :(得分:1)
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
}