我想读个xml,然后对该xml做些修改后再转成InputStream
请问该怎么实现啊??

解决方案 »

  1.   

    是用JDOM读写XML文件吗,给你两段代码,参考一下吧
    package com.bjsxt.drp.util;import java.io.IOException;
    import java.util.Iterator;
    import java.util.List;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.jdom.xpath.XPath;
    /**读XML文件
     *<?xml version="1.0" encoding="UTF-8"?>
     * <config>
     * <jdbc>
     * <driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name>
     * <url>jdbc:oracle:thin:@127.0.0.1:1521:ORACLE9</url>
     * <user-name>drp</user-name>
     * <password>drp</password>
     * </jdbc>
     * <beans>
     * <bean id="com.bjsxt.drp.basedata.dao.ItemDao" class="com.bjsxt.drp.basedata.dao.ItemDao4OracleImpl"/>
     * <bean id="com.bjsxt.drp.flowcard.dao.FlowCardDao" class="com.bjsxt.drp.flowcard.dao.impl.FlowCardDao4OracleImpl"/>
     * </beans>
     * </config>
     */
    public class ConfigReader {

    private static final String CONFIG_FILE_NAME="drp-config.xml";  private static ConfigReader instance = new ConfigReader();

    private ConfigReader() {
    SAXBuilder sb = new SAXBuilder();
    try {
    Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE_NAME));
    Element rootElt = doc.getRootElement();
    Element driverClassNameElt = (Element)XPath.selectSingleNode(rootElt, "//config/jdbc/driver-class-name");
    System.out.println("driverClassName=" + driverClassNameElt.getText()); 

    Element urlElt = (Element)XPath.selectSingleNode(rootElt, "//config/jdbc/url");
    System.out.println("url=" + urlElt.getText());

    Element userNameElt = (Element)XPath.selectSingleNode(rootElt, "//config/jdbc/user-name");
    System.out.println("username=" + userNameElt.getText()); Element passwordElt = (Element)XPath.selectSingleNode(rootElt, "//config/jdbc/password");
    System.out.println("password=" + passwordElt.getText());

    List beanList = XPath.selectNodes(rootElt, "//config/beans/bean");
    for (Iterator iter=beanList.iterator(); iter.hasNext();) {
    Element beanElt = (Element)iter.next();
    System.out.println("id=" + beanElt.getAttributeValue("id"));
    System.out.println("class=" + beanElt.getAttributeValue("class"));
    }
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new java.lang.RuntimeException();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new java.lang.RuntimeException();
    }
    }

    public static ConfigReader getInstance() {
    return instance;
    }

    public static void main(String[] args) {
    ConfigReader.getInstance();
    }
    }
    package com.bjsxt.drp.util;import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;public class XmlWriter { /**创建XML文件
     * <items>
     *  <item>
     *  <id>6</id>
     *  <name>吉林省</name>
     *      </item>
     *  <item>
     *  <id>7</id>
     *  <name>辽宁省</name>
     *      </item>
     *  <item>
     *  <id>8</id>
     *  <name>黑龙江</name>
     *      </item>
     * </items>
     */
    public static void main(String[] args) {
    Element rootElt = new Element("items");

    Element itemElt = new Element("item");

    Element idElt = new Element("id");
    idElt.addContent("6");

    Element nameElt = new Element("name");
    nameElt.addContent("吉林省");

    itemElt.addContent(idElt);
    itemElt.addContent(nameElt);

    rootElt.addContent(itemElt);

    Document doc = new Document(rootElt);

    XMLOutputter out = new XMLOutputter();
    out.setFormat(Format.getCompactFormat().setEncoding("GBK"));

    String xmlString = out.outputString(doc);//转换为字符串
    System.out.println(xmlString);

    try {
    out.output(doc, new FileOutputStream("c:/testxml.xml"));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}