java dom getTextContent() issue

喜夏-厌秋 提交于 2019-12-02 01:50:21
javanna

I've tried your code with your xml, and it prints out the whole text content for me, very strange. Anyway, the Node#getTextContext method returns the text content of the current node and its descendants. I suggest you to use node.getFirstChild().getNodeValue(), which prints out the text content for your node and not its descendants. An other way is iterating over the children of the Suburbs node. You should also take a look here.

This is my main which prints out the same text for two times, using both getFirstChild().getNodeValue() and getChildNodes().item(i).getNodeValue():

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException  {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new File("dom.xml"));

    NodeList nodeList = doc.getElementsByTagName("Suburb");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.hasChildNodes()) {

            System.out.println("<tr><td>Suburb</td>" + "<td>"+node.getFirstChild().getNodeValue()+"</td></tr>");

            NodeList textNodeList = node.getChildNodes();
            StringBuilder textBuilder = new StringBuilder();
            for (int j = 0; j < textNodeList.getLength(); j++) {
                Node textNode = textNodeList.item(j);
                if (textNode.getNodeType() == Node.TEXT_NODE) {
                    textBuilder.append(textNode.getNodeValue());
                }
            }
            System.out.println("<tr><td>Suburb</td>" + "<td>" + textBuilder.toString() + "</td></tr>");
        }
    }
}

This is my output with your xml:

<tr><td>Suburb</td><td>Bondi Junction</td></tr>
<tr><td>Suburb</td><td>Bondi Junction</td></tr>

Try iterating over children of suburb1 and concatenation value of all contained text nodes. The getTextContent() method is very problematic in most DOM implementations. It rarely does what developers think it should do.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!