如何只解析特定的标签值?

时间:2013-08-05 13:24:35

标签: android xml

我只想解析与Margherita标记相关的值。可以解析与标签相关的值吗?

<?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>

在上面的XML中,我想只显示与Margherita标签相关的值。这有可能吗?如果是这样,怎么样?

3 个答案:

答案 0 :(得分:2)

这是一个示例代码,它是一个简单的java项目,而不是android,但它对我有用!

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();
    }
}
}

答案 1 :(得分:1)

我建议使用Simple库,并建模一些数据类以绑定到xml。

项目类:

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;
}

菜单类:

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;
}

将其反序列化为对象:

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--
        }

    }

}

答案 2 :(得分:0)

在你的情况下,sax-parser将是更好的选择:

http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

因为您可以“跳过”您不想使用的项目。