doc = db.parse(docFile); // STEP 1: Get the root element Element root = doc.getDocumentElement(); System.out.println("The root element is " + root.getNodeName());
// STEP 2: Get the children NodeList children = root.getChildNodes(); System.out.println("There are "+children.getLength() +" nodes in this document.");
// STEP 4: Recurse this functionality(递归) stepThrough(root);
我的xm文档在上面,程序输出也在上面,我想输出是 The root element is orders There are 2 nodes in this docume 因为跟结点orders下如果忽略空白结点的话只有两个子结点(order和order),忽略子结点的语句该怎么写呢/?如果不忽略的话就有5个子结点了!dbf.setIgnoringElementContentWhitespace(true)怎么不行?
package com.dom;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.*;
public class OrderProcessor {
private static void stepThrough(Node start)
{ System.out.println(start.getNodeName()+" = "+start.getNodeValue());
/*通过将节点的 nodeType 与常量 ELEMENT_NODE 作比较,从而确定它是否为一个元素。
Node 对象带有成员常量,它们表示每种类型的节点,比如 ELEMENT_NODE 或 ATTRIBUTE_NODE。
如果 nodeType 与 ELEMENT_NODE 匹配,它就是一个元素。
对于找到的每个元素,应用程序都会创建一个包含该元素的所有属性的 NamedNodeMap。
应用程序能够迭代 NamedNodeMap,打印每个属性的名称和值,就像它迭代 NodeList 一样。
*/
if (start.getNodeType() == start.ELEMENT_NODE)
{
NamedNodeMap startAttr = start.getAttributes();
for (int i = 0; i < startAttr.getLength();i++) {
Node attr = startAttr.item(i);
System.out.println(" Attribute: "+ attr.getNodeName()
+" = "+attr.getNodeValue());
}
}
// for 循环首先从根元素的第一个孩子开始。 应用程序迭代第一个孩子的所有兄弟,直至已全部对它们求值
for (Node child = start.getFirstChild();
child != null;
child = child.getNextSibling())
{
stepThrough(child); }
} public static void main(String[] args) {
File docFile = new File("h://orders.xml");
Document doc = null;
try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 这句话dbf.setIgnoringElementContentWhitespace(true);怎么没起作用? dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
// STEP 1: Get the root element Element root = doc.getDocumentElement();
System.out.println("The root element is " + root.getNodeName());
// STEP 2: Get the children
NodeList children = root.getChildNodes();
System.out.println("There are "+children.getLength()
+" nodes in this document.");
// STEP 4: Recurse this functionality(递归)
stepThrough(root);
} catch (Exception e) {
System.out.print("Problem parsing the file: "+e.getMessage());
}
}
}The root element is orders
There are 5 nodes in this document.
orders = null
#text = order = null
#text =
customerid = null
Attribute: limit = 1000
#text = 12341
#text =
status = null
#text = pending
#text =
item = null
Attribute: instock = Y
Attribute: itemid = SA15
#text =
name = null
#text = Silver Show Saddle, 16 inch
#text =
price = null
#text = 825.00
#text =
qty = null
#text = 1
#text =
#text =
item = null
Attribute: instock = N
Attribute: itemid = C49
#text =
name = null
#text = Premium Cinch
#text =
price = null
#text = 49.00
#text =
qty = null
#text = 1
#text =
#text =
#text =
order = null
#text =
customerid = null
Attribute: limit = 150
#text = 251222
#text =
status = null
#text = pending
#text =
item = null
Attribute: instock = Y
Attribute: itemid = WB78
#text =
name = null
#text = Winter Blanket (78 inch)
#text =
price = null
#text = 20
#text =
qty = null
#text = 10
#text =
#text =
#text =
DocumentBuilder db = dbf.newDocumentBuilder().;
Document doc = db.parse(docFile); // docFile is a Xml File
doc.normalize(); //就是这句话了。
另外,success_dream(程序人生) success_dream(程序人生)
doc.normalize(); 这句话一定要加上!!!!
我先用dbf.setIgnoringElementContentWhitespace(true)就已经忽略了空白结点了,然后在用
root.getChildNodes()计算的结点已经不含有空白结点了啊!!
1)dbf.setIgnoringElementContentWhitespace(true)就已经忽略了空白结点了
2)root.getChildNodes()计算的结点已经不含有空白结点了
这样做不是很好吗?
to 楼主,为什么不用JDOM呢?JDOM不会出现这个问题啊。
to cds27(http://blog.csdn.net/cds27/) 你叫人家用什么什么什么 jdom ,不要误人子弟了吧
首先jdom 不是标准,就算要用也用dom4j,人家楼主要用sun 的 jaxp 有什么错啊。
你看的那本书是什么呀,书也有说错的时候。
你是要什么效果,说清楚点啊
The root element is orders
There are 2 nodes in this docume
因为跟结点orders下如果忽略空白结点的话只有两个子结点(order和order),忽略子结点的语句该怎么写呢/?如果不忽略的话就有5个子结点了!dbf.setIgnoringElementContentWhitespace(true)怎么不行?