这段代码只回复了一个"问题"标签的元素,但我在xml文件中有另外9个问题元素。这里有什么错误的东西吗?我需要循环。因为当我检查循环时,它只循环一次。问题是什么?我想出来了。
这是我的xml:
<Results>
<question>
<eno>3</eno>
<qno>1</qno>
<qtext>The Battle of Gettysburg was fought during which war?</qtext>
<correctAnswer>C</correctAnswer>
</question>
<question>
<eno>3</eno>
<qno>2</qno>
<qtext>Neil Armstrong and Buzz Aldrin walked how many
minutes on the moon in 1696?</qtext>
<correctAnswer>B</correctAnswer>
</question>
</Results>
我的源代码:
NodeList listOfQuestions = doc.getElementsByTagName("question");
for(int s=0; s<listOfQuestions.getLength(); s++)
{
System.out.println(listOfQuestions.getLength());
Node firstQuestionNode = listOfQuestions.item(0);
if(firstQuestionNode.getNodeType() == Node.ELEMENT_NODE){
Element firstQElement = (Element)firstQuestionNode;
NodeList enoList = firstQElement.getElementsByTagName("eno");
Element enoElement =(Element)enoList.item(s);
NodeList enosList = enoElement.getChildNodes();
String eno=((Node)enosList.item(s)).getNodeValue().trim();
System.out.println(eno);
NodeList qnoList = firstQElement.getElementsByTagName("qno");
Element qnoElement =(Element)qnoList.item(s);
NodeList qnosList = qnoElement.getChildNodes();
String qno= ((Node)qnosList.item(s)).getNodeValue().trim();
System.out.println(qno);
NodeList qtextList = firstQElement.getElementsByTagName("qtext");
Element qtextElement =(Element)qtextList.item(s);
NodeList qtextsList = qtextElement.getChildNodes();
String qtext= ((Node)qtextsList.item(s)).getNodeValue().trim();
System.out.println(qtext);
NodeList correctAnswerList = firstQElement.getElementsByTagName("correctAnswer");
Element correctAnswerElement =(Element)correctAnswerList.item(s);
NodeList correctAnswerElementList = correctAnswerElement.getChildNodes();
String correctAnswer= ((Node)correctAnswerElementList.item(s)).getNodeValue().trim();
System.out.println(correctAnswer);
int i=st.executeUpdate("insert into question(eno,qno,qtext,correctAnswer) values('"+eno+"','"+qno+"','"+qtext+"','"+correctAnswer+"')");
System.out.println("s is"+s);
}
}
答案 0 :(得分:0)
你有硬编码
Button
我认为你打算在那里使用变量Node firstQuestionNode = listOfQuestions.item(0);
^^^
......或许不是,很难说你想要做什么。无论如何,没有其他对s
的引用,除了第一个节点之外,你永远不会检索任何节点。
答案 1 :(得分:0)
你应该看一下jsoup,它是一个专为在java中解析HTML DOM代码而构建的API,并且有很多额外的功能。此外,您当前尝试提取的内容不会超过使用API组件的3-4 LOC。
在他们的网站上查看他们与URL连接的示例,并且获取DOM-Elements只有2个LOC:
> New-SshSession -ComputerName 10.0.0.xx -Username username-Password
> jenkins@123 Invoke-SshCommand -ComputerName 10.0.0.xx -Command "cd
> docker_CIServiceApp ; unzip prod.zip -d prod/"