<?xml version="1.0" encoding="UTF-8" ?>
<书架>
<书>
<书名>魔兽世界法师攻略</书名>
<作者>皮卡丘</作者>
<售价>100.00</售价>
</书>
<书>
<书名>魔兽世界萨满攻略</书名>
<作者>杰尼龟</作者>
<售价>100.00</售价>
</书>
</书架>package com.wow.test1;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class Test {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder =factory.newDocumentBuilder();
Document document =builder.parse("src\\book.xml");
NodeList list=document.getElementsByTagName("书");
System.out.println(list.item(0).getTextContent());
System.out.println(list.item(0).getFirstChild().getNodeValue());
}
}小弟想问的是list.item(0).getTextContent()和list.item(0).getFirstChild().getNodeValue()的区别是什么?
为什么getElementsByTagName("书");的时候list.item(0).getFirstChild().getNodeValue()没有打印的内容?

解决方案 »

  1.   

    情况有点复杂,跟XML规范有关。XML中,只有 属性、备注、文本,这几种节点才有nodeValue。而文本,不是你理解的:
      “<书名>魔兽世界法师攻略</书名>”
    这个东西,而是<书名>这个节点之内还有一个文本节点是:
      “魔兽世界法师攻略” 比如有节点为:
      <书名>魔兽世界<强调>法师</强调>攻略</书名>
    那么书名下面有三个字节点,分别为:
      文本节点:魔兽世界
      元素节点:<强调>
      文本节点:攻略
    说了半天不知道你明白了没有,总的来说应该是:
      System.out.println(list.item(0).getFirstChild().getFirstChild().getNodeValue());
    或者:
      System.out.println(list.item(0).getFirstChild().getTextContent());
      

  2.   


    package com.wow.test1;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;public class Test {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    DocumentBuilder builder =factory.newDocumentBuilder();
    Document document =builder.parse("src\\book.xml");
    NodeList list=document.getElementsByTagName("书");
    //System.out.println(list.item(0).getTextContent());
    for(int x=0;x<list.getLength();x++){
    System.out.println(list.item(0).getFirstChild().getFirstChild().getNodeValue());
    }
    }
    }Exception in thread "main" java.lang.NullPointerException
    at com.wow.test1.Test.main(Test.java:21)
    不行哦,出异常了
      

  3.   

    可能是这里的问题吧 
    System.out.println(list.item(0).getFirstChild().getFirstChild().getNodeValue());改成下面的试试
    System.out.println(list.item(0).getChildNodes().item(1).getFirstChild().getNodeValue());
      

  4.   

    System.out.println(list.item(0).getFirstChild().getFirstChild().getNodeValue());出错的原因是什么
      

  5.   

    我运行为啥没问题?
            ByteArrayInputStream bais = new ByteArrayInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<书架><书><书名>魔兽世界法师攻略</书名><作者>皮卡丘</作者><售价>100.00</售价></书><书><书名>魔兽世界萨满攻略</书名><作者>杰尼龟</作者><售价>100.00</售价></书></书架>".getBytes());
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(bais);
            NodeList list = document.getElementsByTagName("书");
            for (int x = 0; x < list.getLength(); x++) {
                System.out.println(list.item(x).getFirstChild().getFirstChild().getNodeValue());
            }
    结果:
    魔兽世界法师攻略
    魔兽世界萨满攻略
      

  6.   

    因为你的xml跟他的不一样。
    他的xml中都有换行的 ,那也是一个文本节点
      

  7.   

    getTextContent()
    这个方法和他们的区别是啥?
      

  8.   

     String getTextContent() 
              此属性返回此节点及其后代的文本内容。 api里面是这样写得 。 我没试过。
      

  9.   

    API小弟也看过,就是不知道吗意思?不知道小弟这么理解是否正确
      

  10.   

    This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. On setting, any possible children this node may have are removed and, if it the new string is not empty or null, replaced by a single Text node containing the string this attribute is set to. On getting, no serialization is performed, the returned string does not contain any up. No whitespace normalization is performed and the returned string does not contain the white spaces in element content (see the attribute Text.isElementContentWhitespace). Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content. The string returned is made of the text content of this node depending on its type, as defined below:◎ ELEMENT_NODE, ATTRIBUTE_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODE
    concatenation of the textContent attribute value of every child node, excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes. This is the empty string if the node has no children.◎ TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE
    nodeValue◎ DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE
    null