我正在尝试解析 xml 文件(来自 Assets ),但是我无法正确解析。
通常当我这样做时,我会格式化我的 xml 文件,如下所示:
<channel>
<item>
<name>Hello Earth</name>
<place>Earth</place>
</item>
<item>
<name>Goodbye Mars</name>
<place>Mars</place>
</item/>
</channel>
我将使用以下代码来解析我需要的内容:
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("words.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(inputStream);
org.w3c.dom.Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("item");
String name="";
String place="";
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String propertyName = property.getNodeName();
if (propertyName.equalsIgnoreCase("name")) {
String strText = property.getFirstChild()
.getNodeValue();
nam = strText;
}
if (propertyName.equalsIgnoreCase("place")) {
String strText = property.getFirstChild()
.getNodeValue();
place = strText;
}
db.insertWord(name, place);
}
然而,xml 来自另一个源,并且格式不同,它使用属性,因为它是 mysql 导出。这是我必须解析的 XML,但我不知道该怎么做,我尝试使用 getAttribute() 方法,但无法从 xml 中获取数据:
<table name="item">
<column name="name">Hello Earth</column>
<column name="place">Earth</column>
</table>
<table name="item">
<column name="name">Goodbye Mars</column>
<column name="place">Mars</column>
</table>
最佳答案
您可以使用拉解析器尝试此操作,如下所示..
//定义类项
class Item
{
String name;
String place;
}
ArrayList<Item>items=new ArrayList<Item>();
void somemethod()
{
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("words.xml");
Item item=new Item();
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(inputStream,null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{ }
else if(eventType == XmlPullParser.START_TAG)
{
try
{
if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("name"))
{
eventType = xpp.next();
item.name=Integer.parseInt(xpp.getText().toString());
}
else if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("place"))
{
eventType = xpp.next();
item.place=xpp.getText().toString();
}
}
catch (Exception e)
{
//e.printStackTrace();
}
}
else if(eventType == XmlPullParser.END_TAG)
{
if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("content"))
{
eventType = xpp.next();
items.put(item);
item=new Item();
}
}
else if(eventType == XmlPullParser.TEXT)
{}
eventType = xpp.next();
}// end of while
}
catch (XmlPullParserException e)
{
//e.printStackTrace();
}
catch (IOException e)
{
//e.printStackTrace();
}
finally
{
try
{
if(inputStream!=null)
inputStream.close();
} catch (IOException e)
{
}
}
}
关于java - 在 Android 中使用属性解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12355808/