日期:2014-05-20  浏览次数:20624 次

读取XML
求大大帮忙看一下这个读取xml中的if (!name.equals("#text"))  
这个#text是啥意思
Java code

private Map read(InputStream is) {
        Map map = new HashMap();
        try {
            Document d = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder().parse(is);

            Element root = d.getDocumentElement();
            NodeList nList = root.getElementsByTagName("client");
            String name, value = null;
            for (int i = 0; i < nList.getLength(); i++) {
                NodeList nList2 = nList.item(i).getChildNodes();
                for (int j = 0; j < nList2.getLength(); j++) {
                    name = nList2.item(j).getNodeName();// /nList2:port,location,ip
                    if (!name.equals("#text")) {
                        if (d.getElementsByTagName(name).item(i)
                                .getFirstChild() != null) {
                            value = d.getElementsByTagName(name).item(i)
                                    .getFirstChild().getNodeValue();
                        } else {
                            value = "";
                        }
                        map.put(name, value);
                    }
                }
            }
        } catch (Exception e) {

        }
        return map;
    }



------解决方案--------------------
这个与库文件有关系把! 你看一下他的库文件的写法!
------解决方案--------------------
Java API 里的:
Interface nodeName nodeValue attributes
Text "#text" content of the text node null

#text是指这个节点的内容,也就是127.0.0.1、9999、D:\xd\mmc这些内容。

这个东西我没试过,刚刚看文档学到的,楼主自己试一下吧,记得说一下是不是这样的,谢谢了……