Android 遍历嵌套的xml文件
我试图在Android中遍历一个嵌套的XML字符串,如下所示:Android 遍历嵌套的xml文件,android,xml,Android,Xml,我试图在Android中遍历一个嵌套的XML字符串,如下所示: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Results> <Result> <Questions> <Question>Where can I get the local variable</Question> <Answ
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Results>
<Result>
<Questions>
<Question>Where can I get the local variable</Question>
<Answer>In the local method</Answer>
<AverageRating>3.0</AverageRating>
</Questions>
<Keywords>
<Keyword>Methods</Keyword>
<Keyword>Returns</Keyword>
<Keyword>Void</Keyword>
</Keywords>
</Result>
<Result>
<Questions>
<Question>How can I do a nested for loop</Question>
<Answer>Easy</Answer>
<AverageRating>2.5</AverageRating>
</Questions>
<Keywords>
<Keyword>Methods</Keyword>
<Keyword>Returns</Keyword>
<Keyword>Void</Keyword>
<Keyword>Methods</Keyword>
<Keyword>Returns</Keyword>
</Keywords>
</Result>
String XMLin;
XmlPullParserFactory factory;
String tag;
ArrayList<Question> questions = new ArrayList<Question>();
try {
XMLin = readString(instream);
} catch (IOException e1) {
// TODO Auto-generated catch block
XMLin = "Something went wrong";
}
try {
// Set up the Class that will be parsing the xml file
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader (XMLin));
// Get the first event type
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
// While we have not reached the end of the document, check for start tags
if (eventType == XmlPullParser.START_TAG) {
tag = xpp.getName();
if (tag.equals("Questions") && eventType == XmlPullParser.START_TAG) {
Question question = new Question();
do {
if (tag.equals("Question")) {
// Add question text to question
eventType = xpp.next();
String text = xpp.getText(); // Text between tags
} else if (tag.equals("Answer")) {
eventType = xpp.next();
String text = xpp.getText(); // Text between tags
} else if (tag.equals("AvergaeRating") {
eventType = xpp.next();
String text = xpp.getText(); // Text between tags
}
eventType = xpp.next();
if (eventType == XmlPullParser.TEXT) {
tag = xpp.getText();
} else {
tag = xpp.getName();
}
} while (!tag.equals("Questions"))
questions.add(question);
}
}
}
在哪里可以得到局部变量
局部法
3
方法
退换商品
无效的
如何执行嵌套for循环
容易的
2.5
方法
退换商品
无效的
方法
退换商品
使用以下Android代码:
try
{
//Creates the document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(serverResult)));
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
document.getDocumentElement().normalize();
//Look at root node's type (e.g. <query> or <login> or <add>)
String rootNode = document.getDocumentElement().getNodeName().toString();
if (rootNode.equals("Results"))
{
String Question = "";
String Answer = "";
String AverageRating = "";
float rating = 0;
String keyword = "";
NodeList nList = document.getElementsByTagName("Result");
for (int i = 0; i < nList.getLength(); i++)
{
Node nodes = nList.item(i);
if (nodes.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) nodes;
NodeList list = document.getElementsByTagName("Questions");
for (int value = 0; value < list.getLength(); value++)
{
Node node = list.item(value);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
Question = getValue("Question", eElement);
Answer = getValue("Answer", eElement);
AverageRating = getValue("AverageRating", eElement);
rating = Float.parseFloat(AverageRating);
}
}
}
NodeList keywordNode = document.getElementsByTagName("Keywords");
String keywords = "";
for (int y = 0; y < keywordNode.getLength(); y++)
{
Node node = keywordNode.item(y);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
NodeList ModList = document.getElementsByTagName("Keyword");
int count = ModList.getLength();
for (int b = 0; b < count; b++)
{
keyword = element.getElementsByTagName("Keyword").item(b).getTextContent();
keywords = keywords + keyword + "\n";
}
}
items.add(new Question(Question, Answer, rating, keywords));
}
}
}
}
catch (Exception e)
{
String s = e.getMessage();
publishProgress(s);
}
试试看
{
//创建文档
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdocument=builder.parse(新的InputSource(新的StringReader(serverResult));
//可选,但推荐
//请阅读以下内容-http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
document.getDocumentElement().normalize();
//查看根节点的类型(例如or)
字符串rootNode=document.getDocumentElement().getNodeName().toString();
if(rootNode.equals(“结果”))
{
字符串问题=”;
字符串答案=”;
字符串平均值=”;
浮动评级=0;
字符串关键字=”;
NodeList nList=document.getElementsByTagName(“结果”);
对于(int i=0;i
我试图实现的是,对于XML的Result
标记中每个结果的每个问题,我希望获得问题(及其详细信息)和相应的关键字,将每个问题添加到question
类中,然后重复results
标记中的下一个结果。有人能帮我修改代码并告诉我哪里出了问题吗?尝试导入以下内容:
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
可在此网站下载:
然后,尝试以下代码结构:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Results>
<Result>
<Questions>
<Question>Where can I get the local variable</Question>
<Answer>In the local method</Answer>
<AverageRating>3.0</AverageRating>
</Questions>
<Keywords>
<Keyword>Methods</Keyword>
<Keyword>Returns</Keyword>
<Keyword>Void</Keyword>
</Keywords>
</Result>
<Result>
<Questions>
<Question>How can I do a nested for loop</Question>
<Answer>Easy</Answer>
<AverageRating>2.5</AverageRating>
</Questions>
<Keywords>
<Keyword>Methods</Keyword>
<Keyword>Returns</Keyword>
<Keyword>Void</Keyword>
<Keyword>Methods</Keyword>
<Keyword>Returns</Keyword>
</Keywords>
</Result>
String XMLin;
XmlPullParserFactory factory;
String tag;
ArrayList<Question> questions = new ArrayList<Question>();
try {
XMLin = readString(instream);
} catch (IOException e1) {
// TODO Auto-generated catch block
XMLin = "Something went wrong";
}
try {
// Set up the Class that will be parsing the xml file
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader (XMLin));
// Get the first event type
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
// While we have not reached the end of the document, check for start tags
if (eventType == XmlPullParser.START_TAG) {
tag = xpp.getName();
if (tag.equals("Questions") && eventType == XmlPullParser.START_TAG) {
Question question = new Question();
do {
if (tag.equals("Question")) {
// Add question text to question
eventType = xpp.next();
String text = xpp.getText(); // Text between tags
} else if (tag.equals("Answer")) {
eventType = xpp.next();
String text = xpp.getText(); // Text between tags
} else if (tag.equals("AvergaeRating") {
eventType = xpp.next();
String text = xpp.getText(); // Text between tags
}
eventType = xpp.next();
if (eventType == XmlPullParser.TEXT) {
tag = xpp.getText();
} else {
tag = xpp.getName();
}
} while (!tag.equals("Questions"))
questions.add(question);
}
}
}
stringxmlin;
XmlPullParserFactory工厂;
字符串标签;
ArrayList问题=新建ArrayList();
试一试{
XMLin=读取字符串(流内);
}捕获(IOE1异常){
//TODO自动生成的捕捉块
XMLin=“出了问题”;
}
试一试{
//设置将解析xml文件的类
factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
setInput(新的StringReader(XMLin));
//获取第一个事件类型
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_文档){
//虽然我们还没有到达文档的结尾,但请检查开始标记
if(eventType==XmlPullParser.START_标记){
tag=xpp.getName();
if(tag.equals(“Questions”)&&eventType==XmlPullParser.START_标记){
问题=新问题();
做{
如果(标记等于(“问题”)){
//向问题添加问题文本
eventType=xpp.next();
String text=xpp.getText();//标记之间的文本
}else if(标记等于(“回答”)){
eventType=xpp.next();
String text=xpp.getText();//标记之间的文本
}else if(标记等于(“平均值”){
eventType=xpp.next();
String text=xpp.getText();//标记之间的文本
}
eventType=xpp.next();
if(eventType==XmlPullParser.TEXT){
tag=xpp.getText();
}否则{
tag=xpp.getName();
}
}而(!tag.equals(“问题”))
问题.加入(问题);
}
}
}
这是我用来通过XML解析的一个修改示例