当前位置: 技术问答>java相关
用Java解析xml时,遇到了一个这样的问题!
来源: 互联网 发布时间:2015-03-28
本文导语: 大家好,我是一个Java和XML的初学者,昨天我用Java解析XML的DOM树时,遇到一个问题,十分苦恼.请各位大虾帮帮忙,看我的问题出在哪里. 我的开发环境是JBuilder4.0,JDK1.3,JAXP1.0,从sun公司下载的.我想解析DOM树,再解析前,想无...
大家好,我是一个Java和XML的初学者,昨天我用Java解析XML的DOM树时,遇到一个问题,十分苦恼.请各位大虾帮帮忙,看我的问题出在哪里.
我的开发环境是JBuilder4.0,JDK1.3,JAXP1.0,从sun公司下载的.我想解析DOM树,再解析前,想无视空白区域,所以我吧DocumentBuilderFactory的WhiteSpace设成了true,即setIgnoringElementContentWhitespace(true);可是程序运行时,还是把空白区间当作一个Node处理了.后来我又把setIgnoringElementContentWhitespace(false)试了一下,结果还是一样,也就是setIgnoringElementContentWhitespace(boolean)这个方法没起作用.后来我有检查了一下setValidating(boolean)这个方法,无论它是true还是false,空白区间还是去不掉,请问大家这是怎么回事?我真的不想把空白区间当作Node处理.微软提供的msxml却没有这个问题,只要把preserveWhiteSpace设定就可以了.我的源代码和xml文件是这样的.
--------------------------------------------------------------------------
import java.io.*;
import java.util.jar.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
/**
* It is a example that to load xml file
* you can get the interface JAXP from http://java.sun.com/xml/
* @author Tosan
* @version 1.0 write at 2002.01.22
*/
public class HelloWorld {
public static void main(String[] args) {
try {
int chlNum;
String nName;
String nValue;
Node nNode;
NodeList nList;
//get the file name and path
String userDir = System.getProperty("user.dir");
String sFileName = userDir + "\TestLoad.xml";
//new a DocumentBuilderFactory thread
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//don't verify the DTD
dbf.setValidating(false);
//don't read comments
dbf.setIgnoringComments(true);
//don't verify the whitespace ???????????????
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
//get the document file
Document doc = builder.parse(sFileName);
//get the dom tree
nList = doc.getChildNodes();
nNode = nList.item(0);//root node
nName = nNode.getNodeName();
nValue = nNode.getNodeValue();
nList = nNode.getChildNodes();
chlNum = nList.getLength(); //length = 3, I think it is wrong. I want to it is 1!!!!
for (int i = 0; i
我的开发环境是JBuilder4.0,JDK1.3,JAXP1.0,从sun公司下载的.我想解析DOM树,再解析前,想无视空白区域,所以我吧DocumentBuilderFactory的WhiteSpace设成了true,即setIgnoringElementContentWhitespace(true);可是程序运行时,还是把空白区间当作一个Node处理了.后来我又把setIgnoringElementContentWhitespace(false)试了一下,结果还是一样,也就是setIgnoringElementContentWhitespace(boolean)这个方法没起作用.后来我有检查了一下setValidating(boolean)这个方法,无论它是true还是false,空白区间还是去不掉,请问大家这是怎么回事?我真的不想把空白区间当作Node处理.微软提供的msxml却没有这个问题,只要把preserveWhiteSpace设定就可以了.我的源代码和xml文件是这样的.
--------------------------------------------------------------------------
import java.io.*;
import java.util.jar.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
/**
* It is a example that to load xml file
* you can get the interface JAXP from http://java.sun.com/xml/
* @author Tosan
* @version 1.0 write at 2002.01.22
*/
public class HelloWorld {
public static void main(String[] args) {
try {
int chlNum;
String nName;
String nValue;
Node nNode;
NodeList nList;
//get the file name and path
String userDir = System.getProperty("user.dir");
String sFileName = userDir + "\TestLoad.xml";
//new a DocumentBuilderFactory thread
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//don't verify the DTD
dbf.setValidating(false);
//don't read comments
dbf.setIgnoringComments(true);
//don't verify the whitespace ???????????????
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
//get the document file
Document doc = builder.parse(sFileName);
//get the dom tree
nList = doc.getChildNodes();
nNode = nList.item(0);//root node
nName = nNode.getNodeName();
nValue = nNode.getNodeValue();
nList = nNode.getChildNodes();
chlNum = nList.getLength(); //length = 3, I think it is wrong. I want to it is 1!!!!
for (int i = 0; i