比如下边的这一堆,连个<?xml version="1.0" encoding="ISO-8859-1"?>都不带、、、怎么解析?<header>
abc
</header>
<body>
<package>
<request service="game">
<user cpCode="911" gameId="123456" userId="1234567" deductFee="200" pwd="1234567" >
</user>
</request>
</package>
</body>

解决方案 »

  1.   

    只要标签成对或者单闭合,标签名称大小写一致,并且层级关系清晰,这样应该是没有什么问题的。。
    http://blog.csdn.net/chenghui0317/article/category/1609471
      

  2.   

    我试了、会报错:“The up in the document following the root element must be well-formed.”
    貌似是XML不符合规范之类的、要不你帮忙解析一下试试看、
      

  3.   

    我发现header 和body 同级,也就是说这两个标签都是根节点,但是实际解析过程 getRootElement()返回的根节点对象 都是单个对象的,由此可知:xml中定义的根节点只允许一个,html页面的html也一样,<html>就是根节点呀!~
      

  4.   


    //用的dom4j,自己给加个根节点
    public static void main(String[] args){
    String path = "t.xml";//改成自己的文件路径
    String content = "";
    try {
    content = readFile(path, Charset.forName("UTF-8"));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    content = "<html>" + content + "</html>";
    Document document = null;
    try {
    document = DocumentHelper.parseText(content);
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //Document document = Xml.readXmlFile(path);
    List list = document.selectNodes("//header");
    Element elem = (Element) list.get(0);
    System.out.println(elem.getStringValue());
    }

    /**
     * 读取文件内容返回字符串
     * 
     * @param path
     * @return
     * @throws IOException
     */
    public static String readFile(String path, Charset charset)
    throws IOException {
    FileInputStream fileInput = new FileInputStream(path);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInput,
    charset);
    BufferedReader reader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder("");
    while (true) {
    String temp = reader.readLine();
    if (temp == null) {
    break;
    }
    sb.append(temp);
    }
    reader.close();
    inputStreamReader.close();
    fileInput.close();
    return sb.toString();
    }
      

  5.   

    这个xml没有根节点的是不能用xml解析的,你可以自己在文件上先加入一个根节点,在用xml解析就可以了
      

  6.   

    自己几个root 然后迭代解析。