【问题标题】:XML returned from URL not being parsed correctly从 URL 返回的 XML 未正确解析
【发布时间】:2012-04-12 05:02:43
【问题描述】:

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

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 值为 'Doc is : [#document: null]' 此外,没有抛出异常。 直接导航到 URL 似乎会生成有效的 XML。代码有问题吗?

【问题讨论】:

  • 您似乎没有收到任何回复。

标签: java xml xml-parsing


【解决方案1】:

文档可能正在正确构建,但您期望 Documents toString() 方法输出 XML 内容,但事实并非如此。看看使用Transformer 来输出 XML。

【讨论】:

【解决方案2】:

代码不一定有什么问题;我采用了一个非常相似的工作程序,并打印出 doc 对象并得到相同的输出。

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多