lawrenst 2009-07-20 05: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部