首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

怎么给xml文件添加新的element

2012-12-27 
如何给xml文件添加新的element下面是一个例子, 如何添加新的/propertyproperties/property......pr

如何给xml文件添加新的element
下面是一个例子, 如何添加新的</property>
<properties>

</property>
...
...
<property>
  <name>Workaround</name>
<option>java.property.com.entrust.toolkit.x509.CertVerifier.ForceV1CertAsCA</option>
  <default>true</default>
  <description>thanks</description>
</property>

</properties>

import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;/** * Created by IntelliJ IDEA. * User: Michael * Date: 12/3/11 * Time: 3:16 PM * To change this template use File | Settings | File Templates. */public class XMLApender {    public static void main(String[] args) {        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();        DocumentBuilder docBuilder = null;        OutputStreamWriter outputStreamWriter = null;        try {            docBuilder = docFactory.newDocumentBuilder();            Document doc = docBuilder.parse("C:\\Documents and Settings\\Michael\\My Documents\\bwengine.xml");            Element properties = doc.getDocumentElement();            NodeList nodeList = doc.getElementsByTagName("option");            for(int i=0; i< nodeList.getLength(); i++){                    Node  node = nodeList.item(i);                    String text = node.getTextContent();                   if("java.property.com.entrust.toolkit.x509.CertVerifier.ForceV1CertAsCA".equals(text)){                                     return;                    }            }            FileOutputStream outputStream = new FileOutputStream("C:\\Documents and Settings\\Michael\\My Documents\\bwengine.xml");            outputStreamWriter = new OutputStreamWriter(outputStream);            Node property = doc.createElement("property");            Node name = doc.createElement("name");            name.setTextContent("Workaround");            Node option = doc.createElement("option");            option.setTextContent("java.property.com.entrust.toolkit.x509.CertVerifier.ForceV1CertAsCA");            Node defau1t = doc.createElement("default");            defau1t.setTextContent("true");            Node description = doc.createElement("description");            description.setTextContent("thanks");            property.appendChild(name);            property.appendChild(option);            property.appendChild(defau1t);            property.appendChild(description);            properties.appendChild(property);            Transformer transformer = TransformerFactory.newInstance().newTransformer();            transformer.setOutputProperty(OutputKeys.INDENT, "yes");            StreamResult result = new StreamResult(outputStreamWriter);            DOMSource source = new DOMSource(doc);            transformer.transform(source, result);            outputStreamWriter.flush();            outputStreamWriter.close();            String xmlString = result.getWriter().toString();            System.out.println(xmlString);        } catch (ParserConfigurationException e) {            e.printStackTrace();        } catch (SAXException e) {            e.printStackTrace();        } catch (TransformerConfigurationException e) {            e.printStackTrace();        } catch (TransformerException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

热点排行