上面输出结果:
----Student------Node type=3  Name:#text  value:
Node type=1Node type=3  Name:#text  value: 
Node type=1Node type=3  Name:#text  value:
Node type=1Node type=3  Name:#text  value:
----Student------Node type=3  Name:#text  value:
Node type=1Node type=3  Name:#text  value:
Node type=1Node type=3  Name:#text  value:
Node type=1Node type=3  Name:#text  value:为什么用getNodeName()返回的是#text,而getNodeValue()返回的是""

解决方案 »

  1.   

    you should read the API of NodeInterface      nodeName nodeValue attributes 
    Attr           name of attribute value of attribute null 
    CDATASection   "#cdata-section" content of the CDATA Section null 
    Comment        "#comment" content of the comment null 
    Document       "#document" null null 
    DocumentFragment "#document-fragment" null null 
    DocumentType     document type name null null 
    Element          tag name null NamedNodeMap 
    Entity          entity name null null 
    EntityReference   name of entity referenced null null 
    Notation          notation name null null 
    ProcessingInstruction target entire content excluding the target null 
    Text              "#text" content of the text node null 
    understand???
    you can try it in other way :
    for example :
    document.getElement();
      

  2.   

    有明的为什么getChildNodes()会返回一些包含空内容的子结点,我
    试过很多个xml文件,都有如此问题,len2 = list2.getLength();总是返回7,但XML文件中只有3个子项,不知如何解决。
    另如果用getElementsByTagName("name")总能正确返回。
      

  3.   

    空格的原因,具体可以参考Core Java 上面的最后一章XML。
    给你贴出相关的部分:
    To get the element's children (which may be subelements, text, comments, or other nodes), use the getChildNodes method. That method returns a collection of type NodeList. That type was invented before the standard Java collections, and it has a different access protocol. The item method gets the item with a given index, and the getLength method gives the total count of the items. Therefore, you can enumerate all children like this:NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++)
    {
       Node child = children.item(i);
       . . .
    }You have to be careful when analyzing the children. Suppose for example that you are processing the document<font>
       <name>Helvetica</name>
       <size>36</size>
    </font>You would expect the font element to have two children, but the parser reports five:
    The whitespace between <font> and <name>
    The name element 
    The whitespace between </name> and <size> The size element The whitespace between </size> and </font> 
    If you only expect subelements, then you can ignore the whitespace:for (int i = 0; i < children.getLength(); i++)
    {
       Node child = children.item(i);
       if (child instanceof Element)
       {
          Element childElement = (Element)child;
          . . .
       }
    }
      

  4.   

    很感谢 web_spider(蓦然回首,那人却在、灯火阑珊处。) 说的那么详细,不过我如何才能取得<name>Helvetica结点的值呢?如下
    for (int i = 0; i < children.getLength(); i++)
    {
       Node child = children.item(i);
       if (child instanceof Element)
       {
          Element childElement = (Element)child;
          
          String value = childElement.getNodeValue();
         //这里value 为NULL 为什么???
          . . .
       }
    }
      

  5.   

    void testXML(String xmlFile)
      {
        javax.xml.parsers.DocumentBuilder db = null;
        javax.xml.parsers.DocumentBuilderFactory dbf = null;
        Document doc = null;
        Element item = null;
        Element root = null;
        Node node = null;
        NodeList list1 = null;
        NodeList list2 = null;
        try
        {
          dbf = DocumentBuilderFactory.newInstance();
          dbf.setIgnoringElementContentWhitespace(true);
          db = dbf.newDocumentBuilder();
          doc = db.parse(xmlFile);
        }
        catch(Exception e)
        {}
        root = doc.getDocumentElement();
        list1 = root.getChildNodes();
        int len1 = list1.getLength();
        int len2 = 0;
        String name;
        String value;
        for(int i = 0; i < len1; i++)
        {
          node = list1.item(i);
          if(node.getNodeType() != Node.ELEMENT_NODE)
            continue;
          item = (Element)node;
          name = item.getNodeName();
          System.out.println("----" + name + "------");
          list2 = item.getChildNodes();
          len2 = list2.getLength();
          for(int j= 0; j < len2; j++)
          {
            node = list2.item(j);
            int type = node.getNodeType();
            System.out.println("Node type="+ type);
            if(!(node instanceof Element))
              continue;
            Element item1 = (Element)node;
            name = node.getNodeName();
            NodeList list3 = item1.getChildNodes();
            value = "";
            for(int k = 0;k < list3.getLength(); k++)
            {
              node = list3.item(k);
              type = node.getNodeType();
              value = node.getNodeValue();
              if(node instanceof org.w3c.dom.Text)
              {
                Text text = (Text)node;
                value = text.getData();
                 break;
              }
            }
            System.out.println("\t Name:" + name + "\t value:" + value);
           }
        }
      }
    }