比如下边的这一堆,连个<?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>
abc
</header>
<body>
<package>
<request service="game">
<user cpCode="911" gameId="123456" userId="1234567" deductFee="200" pwd="1234567" >
</user>
</request>
</package>
</body>
http://blog.csdn.net/chenghui0317/article/category/1609471
貌似是XML不符合规范之类的、要不你帮忙解析一下试试看、
//用的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();
}