2

我只想解析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标签相关的值。这可能吗?如果是这样,怎么做?

4

3 回答 3

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();
    }
}
}
于 2013-08-05T13:52:12.560 回答
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--
        }

    }

}
于 2013-08-05T14:18:33.767 回答
0

在您的情况下,sax - 解析器将是更好的选择:

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

因为您可以“跳过”无论如何都不想使用的项目。

于 2013-08-05T14:03:15.790 回答