上面输出结果:
----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()返回的是""
----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()返回的是""
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();
试过很多个xml文件,都有如此问题,len2 = list2.getLength();总是返回7,但XML文件中只有3个子项,不知如何解决。
另如果用getElementsByTagName("name")总能正确返回。
给你贴出相关的部分:
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;
. . .
}
}
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 为什么???
. . .
}
}
{
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);
}
}
}
}