3

我需要解析具有类似结构的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 解析的值..但无法像上面的结构那样检索值..

4

1 回答 1

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
}
于 2013-10-31T09:23:22.427 回答