java dom getTextContent() 问题

发布于 2024-10-29 02:28:41 字数 745 浏览 3 评论 0原文

当我尝试从 servlet 的 doGet 方法访问 xml 数据时,它仅输出直到空白的值,包括整个值。

XML 文件:

<RealEstate>
    <Property>
            <Type>Apartment</Type>
            <Bedrooms>2</Bedrooms>
            <Bathrooms>2</Bathrooms>
            <Suburb>Bondi Junction</Suburb>
            <Rent>1000</Rent>
    </Property>
</RealEstate>

然后我从 doGet 中的 Java Servlet 调用 Suburb:

Node suburb1 = doc.getElementsByTagName("Suburb").item(i);
out.println("<tr><td>Suburb</td>" + "<td>"+suburb1.getTextContent()+"</td></tr>");

它只输出“Bondi”而不是“Bondi Junction”

有人知道为什么吗?

when i'm trying to access my xml data from doGet method of my servlet, it only outputs the value up to the white spaces, including the whole value.

XML File:

<RealEstate>
    <Property>
            <Type>Apartment</Type>
            <Bedrooms>2</Bedrooms>
            <Bathrooms>2</Bathrooms>
            <Suburb>Bondi Junction</Suburb>
            <Rent>1000</Rent>
    </Property>
</RealEstate>

I then call up the Suburb from a Java Servlet in doGet:

Node suburb1 = doc.getElementsByTagName("Suburb").item(i);
out.println("<tr><td>Suburb</td>" + "<td>"+suburb1.getTextContent()+"</td></tr>");

and it only outputs "Bondi" instead of "Bondi Junction"

Does anybody know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-11-05 02:28:41

我用你的xml尝试过你的代码,它为我打印出整个文本内容,非常奇怪。无论如何,Node#getTextContext 方法返回当前节点及其后代的文本内容。
我建议您使用node.getFirstChild().getNodeValue(),它会打印出您的节点的文本内容,而不是其后代的文本内容。另一种方法是迭代 Suburbs 节点的子节点。
您还应该看看此处

这是我的主程序,使用 getFirstChild().getNodeValue() 和 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>");
        }
    }
}

这是我的 xml 输出:

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

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>
兮颜 2024-11-05 02:28:41

尝试迭代 suburb1 的子节点和所有包含的文本节点的串联值。 getTextContent() 方法在大多数 DOM 实现中都存在很大问题。它很少做开发人员认为应该做的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文