我在d:/xxx.xml路径下有个xml,怎么读取这个xml转成string

解决方案 »

  1.   

    我只需要把xml内容转换成string就可以了,谁能给个方法
      

  2.   

    用dom4j
    file f =new file(f);
     saxreader r  =new saxreader();
    document document = r.read(f);
    得到一个document对象你想怎么操作都行了
      

  3.   

    拿到document对象怎么转换成string啊。直接toString()是不行的~~
      

  4.   

    真服了楼主了 你先asXML()然后在toString就行了
      

  5.   


    public boolean checkIp(String ip){
    if (ip!= null && !"".equals(ip)) {
    try {
    File file = new File(this.getServletContext().getRealPath("/ips-config.xml"));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(file);
    NodeList nList = doc.getElementsByTagName("value");
    for (int i = 0; i < nList.getLength(); i++) {
    String string = doc.getElementsByTagName("ip").item(i).getFirstChild().getNodeValue();//.item(i).getNodeValue();
    if (ip.equals(string)) {
    return true;
    }
    }
    return false;
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    return false;
    }
    }else {
    return false;
    }
    }
    样例代码给你了,自己变通的改吧
      

  6.   


    <?xml version='1.0' encoding='UTF-8'?>
    <data>
    <value>
    <ip>192.168.0.232</ip>
    </value>
    <value>
    <ip>10.200.1.11</ip>
    </value>
    </data>
      

  7.   

    如果你只是读取成string的格式
    那你直接用File去按照文件流读取就行了
    如果你需要按照xml文件的格式读取
    才会用到dom4j的saxreader
    要是你不会直接用File读取的话
    那你还是回去看看基础再来问吧以上