JDOM解析XMl

 public static void main(String[] args) {
  String str  = "<xml>" +
    "<users>" +
     "<user>" +
      "<id>123</id>" +
      "<name>zhangsan</name>" +
     "</user>" +
     "<info>ceshi</info>" +
    "</users>" +
    "</xml>";
  
  StringReader sReader = new StringReader(str);
  SAXBuilder build = new SAXBuilder();
  InputSource source = new InputSource(sReader);
  
  try {
   Document doc = build.build(source);
   Element root = doc.getRootElement();
   System.out.println("root:"+root.getName());
   List userList = root.getChildren();
   Element et;
   for(int i=0;i<userList.size();i++)
   {
    et = (Element) userList.get(i);
    System.out.println(et.getChild("user").getChild("id").getText());
    System.out.println(et.getChild("info").getText());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

你可能感兴趣的:(xml)