100分求解决dom4j解析网址的XML问题.
朋友叫我帮它做一个解析网址XML的程式
网址XML格式如下:http://www.78car.com/test.jsp
现在遇到的问题是当这个地址是http://www.78car.com/test.jsp的时候程式就会报错,如果把这个文件改成http://www.78car.com/test.xml即可解析,不知道是何种原因.请高手帮忙.
源码如下:
package com.jo.xmls;
import java.io.*;
import java.net.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
public class GetUrlxmls {
private static Document docs = null;
public static void parseWithSAX(URL aURL) throws DocumentException{
SAXReader xmlReader = new SAXReader();
docs = xmlReader.read(aURL);
}
public void query(Document document)throws DocumentException{
/*List list = document.selectNodes( "//Data/Desc ");
for (Iterator iter = list.iterator(); iter.hasNext(); ){
Attribute attribute = (Attribute) iter.next();
String type = attribute.getValue();
System.out.println(type);
} */
try{
// Node node = document.selectSingleNode( "//Data/Code ");
// String sname = node.valueOf( "@Code ");
// System.out.println(sname);
Element root= document.getRootElement();
Iterator it=root.elementIterator();
printIterator(it);
}catch(Exception e){
e.printStackTrace();
}
}
private void printIterator(Iterator it) {
while(it.hasNext()){
Element element=(Element)it.next();
System.out.println(element.getName()+ "\t "+element.getText()+ "\t ");
if(element.elementIterator()!=null){
printIterator(element.elementIterator());
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
GetUrlxmls x = new GetUrlxmls();
try{
GetUrlxmls.parseWithSAX(new URL( "http://www.78car.com/test.jsp "));