具有多个根元素的Android XmlPullParser
本文关键字:Android XmlPullParser 元素 | 更新日期: 2023-09-27 18:35:49
我来自c#背景,是Android的新手。我正在尝试解析从 .NET 生成的 XML 文件。它实际上是一个具有多个表的XMLDataSet。如下图所示
<XMLDataSet xmlns="http://tempuri.org/XMLDataSet.xsd">
<table1>
<row1>value</row1>
<row2>value</row2>
<row3>value</row3>
</table1>
<table1>
<row1>value</row1>
<row2>value</row2>
<row3>value</row3>
</table1>
<table2>
<row1>value</row1>
<row2>value</row2>
<row3>value</row3>
</table2>
<table2>
<row1>value</row1>
<row2>value</row2>
<row3>value</row3>
</table2>
</XMLDataSet>
等等。我想解析这个xml并得到"列表"。在 c# 中,我使用 XMLDataSet 和 xmlDataSet.Tables["table1"] 来解析它并获取列表。但是我无法使用 XmlPullParser在 andriod 中解析具有多个根元素的 XML。我遇到的所有示例都显示了如何解析单个根 XML。有人可以帮助我解析具有多个根的XML文件吗?
试试这个。
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
public class NetActivity extends Activity {
String urls = "Your URL";
// Progress dialog
ProgressDialog pDialog;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
new XmlParsing(urls).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
else
new XmlParsing(urls).execute(new String[]{null});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class XmlParsing extends AsyncTask<String, Void, String> {
// variables passed in:
String urls;
// constructor
public XmlParsing(String urls) {
this.urls = urls;
}
@Override
protected void onPreExecute() {
pDialog = ProgressDialog.show(NetActivity.this, "Fetching Details..", "Please wait...", true);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
URL url;
try {
url = new URL(urls);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("table1");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("row1");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
System.out.println("row1 : "+(((Node) nameList.item(0)).getNodeValue()));
}
NodeList nodeList1 = doc.getElementsByTagName("table2");
for (int i = 0; i < nodeList1.getLength(); i++) {
Node node = nodeList1.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("row1");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
System.out.println("row1 : "+(((Node) nameList.item(0)).getNodeValue()));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// Now we have your JSONObject, play around with it.
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}