2012-03-28 84 views
0

的XML从URL返回:“http://repository.sonatype.org/service/local/data_index?sha1=ba24d5de831911b684c92cd289ed5ff826271824”不被使用此代码正确解析:XML从URL返回不正确解析

public static void main(String args[]){ 

    try { 

     String urlString = "http://repository.sonatype.org/service/local/data_index?sha1=ba24d5de831911b684c92cd289ed5ff826271824"; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(new URL(urlString).openStream()); 

     System.out.println("Doc is : "+doc); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

正在输出的文档值是'Doc是:[#document:null]'另外,没有任何异常被抛出。 直接导航到URL似乎产生有效的XML。代码有问题吗?

+0

看来你没有得到任何回应。 – kosa 2012-03-28 15:12:37

回答

2

该文档可能是正确构建的,但您期望输出XML内容的方法不是这样。看看使用Transformer来输出XML。

+0

谢谢!此链接也有帮助 - http://stackoverflow.com/questions/1384802/java-how-to-indent-xml-generated-by-transformer – 2012-03-28 15:32:55

1

代码没有任何错误;我做了一个非常类似的工作程序,并打印出doc对象并得到相同的输出结果。