lawrenst 2009-07-20 13:12
浏览 218
已采纳

dom读取xml文件的问题

我有一个xml文件:



deltaQuery="select Uid from User where lastmodify > '${dataimporter.last_index_time}'">











该xml文件没有<?xml...>等标签

我想读取出dataSource标签中driver url等值,写了一个类如下:
File file = new File("data_config.xml");

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder=dbf.newDocumentBuilder();

    Document doc=builder.parse(file);

    NodeList dataSource = doc.getElementsByTagName("dataSource");

    System.out.println(dataSource.getLength());
    for(int i=0;i<dataSource.getLength();i++)
    {
        Node node=dataSource.item(i);
        System.out.println(node);
        System.out.println(node.getNodeValue());
    }

总是说读到的null。
求大牛指正。

另外:我想读取所有filed下的column 和name标签的值,该怎么写呢?

  • 写回答

3条回答 默认 最新

  • wanghaolovezlq 2009-07-20 13:58
    关注

    File file = new File("data_config.xml");

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder=dbf.newDocumentBuilder();

    Document doc=builder.parse(file);

    [color=red]//获取文档的根元素,赋值给rootElement变量

    Element rootElement = document.getDocumentElement();

    NodeList dataSource = rootElement.getElementsByTagName("dataSource"); [/color]

    System.out.println(dataSource.getLength());
    for(int i=0;i<dataSource.getLength();i++)
    {
    Node node=dataSource.item(i);
    System.out.println(node);
    System.out.println(node.getNodeValue());
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示