日期:2014-05-20 浏览次数:20909 次
首先说明LZ贴出来的td结束标签是错的。。。<td/>改为</td>,不然读取根节点就报错。。。 改为:即可 public static void readXML3() { // Map<String, Object> htmls = new HashMap<String, Object>(); SAXBuilder sb = new SAXBuilder(); try { Document doc = sb.build(new FileInputStream("f:/test.xml")); // 改为LZ自己的读路径方式 Element root = doc.getRootElement(); List list = root.getChildren("tr"); // 取tr元素 for (int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); List list_td = element.getChildren(); // 取td元素 for (int k = 0; k < list_td.size(); k++) { Element element_td = (Element) list_td.get(k); String name = element_td.getText(); // 取td文本值 System.out.println(name); } } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
------解决方案--------------------
与上面一样。。只是调整格式 方便看: public static void readXML3() { // Map<String, Object> htmls = new HashMap<String, Object>(); SAXBuilder sb = new SAXBuilder(); try { Document doc = sb.build(new FileInputStream("f:/test.xml")); // 改为LZ自己的读路径方式 Element root = doc.getRootElement(); List list = root.getChildren("tr"); // 取tr元素 for (int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); List list_td = element.getChildren(); // 取td元素 for (int k = 0; k < list_td.size(); k++) { Element element_td = (Element) list_td.get(k); String name = element_td.getText(); // 取td文本值 System.out.println(name); } } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }