Reputation: 23
I want to parse only Margherita
tag-related values. It is possible to parse tag-related values?
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<item>
<id>1</id>
<name>Margherita</name>
<cost>155</cost>
<description>Single cheese topping</description>
</item>
<item>
<id>6</id>
<name>Margherita</name>
<cost>155</cost>
<description>Single cheese topping</description>
</item>
<item>
<id>8</id>
<name>Margherita</name>
<cost>1535</cost>
<description>Single cheese topping</description>
</item>
<item>
<id>2</id>
<name>Double Cheese Margherita</name>
<cost>22e5</cost>
<description>Loaded with Extra Cheese</description>
</item>
<item>
<id>3</id>
<name>Fresh Veggie</name>
<cost>110</cost>
<description>Oninon and Crisp capsicum</description>
</item>
<item>
<id>4</id>
<name>Peppy Paneer</name>
<cost>155</cost>
<description>Paneer, Crisp capsicum and Red pepper</description>
</item>
<item>
<id>5</id>
<name>Mexican Green Wave</name>
<cost>445</cost>
<description>Onion, Crip capsicum, Tomato with mexican herb</description>
</item>
</menu>
In the above XML, I want to show only values related to the Margherita
tags. It this possible? If so, how?
Upvotes: 2
Views: 168
Reputation: 29436
I'd advice to use Simple library, and model some data classes to bind to xml.
Item class:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="item")
public class Item{
@Element(name = "id", required = true)
public int id;
@Element(name = "name", required = true)
public String name;
@Element(name = "cost", required = true)
public double cost;
@Element(name = "description", required = true)
public String desc;
}
Menu class:
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import java.util.List;
@Root(name="menu")
public class Menu{
@ElementList(inline = true,type = Item.class)
public List<Item> items;
}
De-serialize it to objects:
Serializer srl = new Persister();
//--- Placed menu.xml in /assets for a test ---
//--- you can obtain it from somewhere else like a web server etc also ---
InputStream ips = getAssets().open("menu.xml");
Menu menu = srl.read(Menu.class,ips);
if(menu != null && menu.items != null){
for (Item i : menu.items){
if(i.name != null && i.name.contains("Margherita")){
//--do something with this item--
}
}
}
Upvotes: 1
Reputation: 15929
In your case a sax - parser would be a better choice:
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
because you can "skip" the items that you don't want to use anyway.
Upvotes: 0
Reputation: 92
Here is a sample code, it's a simple java project, not android, but it works for me!
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class test {
public static void main(String argv[]) {
try {
File fXmlFile = new File("D:/Xml.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("item");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if (eElement.getElementsByTagName("name").item(0)
.getTextContent().equals("Margherita")) {
System.out.println("Id: "
+ eElement.getElementsByTagName("id").item(0)
.getTextContent());
System.out.println("It costs: "
+ eElement.getElementsByTagName("cost").item(0)
.getTextContent());
System.out.println(eElement
.getElementsByTagName("name").item(0)
.getTextContent());
System.out.println("Description:"
+ eElement.getElementsByTagName("description")
.item(0).getTextContent());
System.out.println();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 2