欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jdom解析XML文件

程序员文章站 2022-05-28 11:42:29
...
     Reader reader = new StringReader(XXX.xml);
     InputSource is = new InputSource(reader);
     DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();
     SAXBuilder build = new SAXBuilder();
     Document   doc = build.build(is);
     Element root=doc.getRootElement();
     java.util.List<Element> list = root.getChildren("attribute");
     Element rootelem=null;
     for (Element elem :list) {
     String name = elem.getAttributeValue("displayname");
      String value = elem.getValue();
      String before = elem.getChildText("before");
     oldvalue.put(name, before);
      if (before !=null) {
     String str = value.replace(before, "");
      newvalue.put(name, str);
      } else {
       newvalue.put(name, value);
         }
              }