<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Zhwys>
  <Zhwy JH="B3-4-3" date="2012-02-01">
    <syhd>2.5</syhd>
    <yxhd>2.1</yxhd>
  </Zhwy>
 <Zhwy JH="B3-4-2" date="2012-02-01">
    <syhd>3.5</syhd>
    <yxhd>4.1</yxhd>
  </Zhwy></Zhwys>请问如何修改syhd   其中JH,date 是关键字.

解决方案 »

  1.   

    两种 方法:1:用DOM 解析 形成 节点列表,节点内容与内存地址映射,改节点内容就是修改 内存中的 Document 对象
    然后使用 Document 对象(在内存中已经经过修改):
    document 是 Document document;TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer  transformer =  transformerFactory.newTransformer();
    Source xmlSource = new DOMSource(document);
    Result outputTarget = new StreamResult(new File("F:\\javaweb20120208_workspace\\day3\\orders.xml"));//xm文件所在位置 transformer.transform(xmlSource, outputTarget);//提交覆盖原来的xml2:用 SAX 或PULL 解析 生成实体列表,LIST<类> 实体列表名
    然后 修改 列表中的对应的实体。
    使用修改后的实体列表 用 XmlSerializer 序列化 写回原地址的XML文件 覆盖例子如下:     * @return 生成的xml文件的字符串表示 
         */  
        private String produceXml(){  
              
            StringWriter stringWriter = new StringWriter();  
            ArrayList<Beauty> beautyList = getData();  
            try {  
                // 获取XmlSerializer对象  
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();  
                XmlSerializer xmlSerializer = factory.newSerializer();  
                // 设置输出流对象  
                xmlSerializer.setOutput(stringWriter);              xmlSerializer.startDocument("utf-8", true);  
                xmlSerializer.startTag(null, "beauties");  
                for(Beauty beauty:beautyList){                  xmlSerializer.startTag(null, "beauty");  
                      
                    xmlSerializer.startTag(null, "name");  
                    xmlSerializer.text(beauty.getName());  
                    xmlSerializer.endTag(null, "name");  
                      
                    xmlSerializer.startTag(null, "age");  
                    xmlSerializer.text(beauty.getAge());  
                    xmlSerializer.endTag(null, "age");  
                      
                    xmlSerializer.endTag(null, "beauty");  
                }  
                xmlSerializer.endTag(null, "beauties");  
                xmlSerializer.endDocument();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return stringWriter.toString();  
      
        }