/**
* Test
* <p>
* Date: 2012-03-13,16:25:20 +0800
*
* @version 1.0
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
File f = new File("c:\\a.xml");
BufferedReader rr = new BufferedReader(new InputStreamReader(
new FileInputStream(f)));
StringBuffer buf = new StringBuffer();
String txt = rr.readLine();
while (txt != null) {
buf.append(txt).append("\n");
txt = rr.readLine();
}
/**
* API 使用错误
*/
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new ByteArrayInputStream(buf
.toString().getBytes("utf-8")));
List<?> policyNodes = document.selectNodes("//PolicyList/Policy");
if (policyNodes != null && policyNodes.size() != 0) {
List<String> policys = new ArrayList<String>();
for (int i = 0; i < policyNodes.size(); i++) {
Element e = (Element) policyNodes.get(i);
policys.add(e.attributeValue("ID"));
}
System.out.println(policys);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
* Test
* <p>
* Date: 2012-03-13,16:25:20 +0800
*
* @version 1.0
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
File f = new File("c:\\a.xml");
BufferedReader rr = new BufferedReader(new InputStreamReader(
new FileInputStream(f)));
StringBuffer buf = new StringBuffer();
String txt = rr.readLine();
while (txt != null) {
buf.append(txt).append("\n");
txt = rr.readLine();
}
/**
* API 使用错误
*/
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new ByteArrayInputStream(buf
.toString().getBytes("utf-8")));
List<?> policyNodes = document.selectNodes("//PolicyList/Policy");
if (policyNodes != null && policyNodes.size() != 0) {
List<String> policys = new ArrayList<String>();
for (int i = 0; i < policyNodes.size(); i++) {
Element e = (Element) policyNodes.get(i);
policys.add(e.attributeValue("ID"));
}
System.out.println(policys);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
错误发生的很不应该,我直接用
saxReader.read(xml)了,事实上read接受xml时候的参数含义为systemId,唉,API啊。。人要仔细。。。老了
引用一下这篇:
http://blog.csdn.net/luo_yifan/article/details/6442618