I am trying to parse an xml file (from assets) however I cannot get it right.
Usually when I do this the I would format my xml file like the following:
<channel>
<item>
<name>Hello Earth</name>
<place>Earth</place>
</item>
<item>
<name>Goodbye Mars</name>
<place>Mars</place>
</item/>
</channel>
And I would use the following code to parse out what I need:
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);
}
However the xml is coming from another source and it is formatted differently, it uses attributes instead since it was a mysql export. Here is the XML I must parse but I cannot figure out how I should this, I tried playing around with the getAttribute() methods but cant get the data out of the 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>