0

我正在尝试制作一个 XML 解析器。但是,以下代码仅给出空白输出。getElementValue() 函数中的“heyya”消息被打印出来,它告诉我们返回了空字符串。请帮忙。

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLParser {
        public Document getDomElement(String xml)
    {
        Document doc=null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try{

            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        }catch (ParserConfigurationException e){
            System.out.println(e.getMessage());
            return null;
        } catch(SAXException e){
            System.out.println(e.getMessage());
            return null;
        } catch(IOException e){
            System.out.println(e.getMessage());
            return null;
        }

        return doc;
    }

    public String getValue(Element item, String str)
    {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }

    public final String getElementValue(Node elem)
    {
        Node child;
        if(elem!=null){
            if(elem.hasChildNodes()){
                for(child=elem.getFirstChild(); child!=null; child = child.getNextSibling()){
                    if(child.getNodeType() == Node.TEXT_NODE){
                        return child.getNodeValue();
                    }
                }

            }

        }
        //System.out.println("heyya");
        return "";
    }
}


class createData
{
    static final String KEY_ID = "id";   //parent node
    static final String KEY_NAME = "name";
    static final String KEY_COST = "cost";
    static final String KEY_DESC = "description";

    public void createArrayList()
    {

        //ArrayList<HashMap<String, String>> userData = new ArrayList<HashMap<String, String>>();
        XMLParser parser = new XMLParser();
        //obtain xml as string here
        String xml="<menu>\n\t<item>\n\t\t<id>1</id>\n\t\t<name>Margherita</name>\n\t\t<cost>155</cost>\n\t\t<description>Single cheese topping</description>\n\t</item></menu>\n";
        Document doc = parser.getDomElement(xml);
        NodeList node_L = doc.getElementsByTagName(KEY_ID);


        for(int i=0; i<node_L.getLength(); i++)
        {
            //HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element)node_L.item(i);
            String name = parser.getValue(e, KEY_NAME);
            String cost = parser.getValue(e, KEY_COST);
            String description = parser.getValue(e, KEY_DESC);
            System.out.println(name+" "+cost+" "+description);
            //map.put(KEY_NAME, name);
            //map.put(KEY_COST, cost);
            //map.put(KEY_DESC, description);
            //userData.add(map);
        }
        System.out.println("hello pop!");
        //System.out.println(userData.get(0));
    }

    public static void main(String args[])throws IOException
    {
        createData ob =new createData();
        ob.createArrayList();

    }
}
4

1 回答 1

0

这就是问题。

NodeList node_L = doc.getElementsByTagName(KEY_ID);

你到了id这里,但id只是你的item. 用这个替换它: -

NodeList node_L = doc.getElementsByTagName("item");

注意:-您的父节点是ITEM而不是ID.

您可以ID像获得其他标签一样获得您的标签。

String id = parser.getValue(e, KEY_ID);
于 2013-03-02T10:09:39.970 回答