使用jdom动态设置值为xml

时间:2012-06-11 05:16:28

标签: java jdom

我有没有固定格式的XML我使用org.w3c.dom跟踪代码,以动态地将值设置为相同的XML。

public String generateXML(String[] tags,String[] tagValues,String xmlfilePath){
        String strXML = "";

        try{

            if(tags == null || tagValues == null || xmlfilePath == null){


            }else{


                File file = new File(xmlfilePath);

                if (file.exists()){

                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    org.w3c.dom.Document doc = builder.parse(file);

                    NodeList nodeList = doc.getElementsByTagName("*");

                    int k =0;
                    for (int i=0; i<nodeList.getLength(); i++) {

                        Node node = (Node)nodeList.item(i);

                        if(node.getNodeName().trim().equalsIgnoreCase(tags[k])){
                            node.setTextContent(tagValues[k]);
                            k++;
                        }
                    }

                    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
                    LSSerializer lsSerializer = domImplementation.createLSSerializer();
                    strXML = lsSerializer.writeToString(doc);

                }else{

                }
            }

        }catch (Exception e) {
            e.printStackTrace();

        }


        return strXML;
    }

但它不适用于某些旧版本的JDK,所以我想对JDOM做同样的事情。

怎么可能?每个示例都需要标记名称,但我想创建常用方法。

1 个答案:

答案 0 :(得分:0)

据我所知,这两种方法会做同样的事情:

我正在使用JDOM 1.x因为你想使用'旧的'JDK(我假设在Java5之前)....

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.filter.ElementFilter;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;

public class DOMJDOM {

    /**
     * @param args
     * @throws ParserConfigurationException 
     * @throws IOException 
     * @throws SAXException 
     */
    public static String dom(File file, String[] tags, String[] tagValues) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document doc = builder.parse(file);

        NodeList nodeList = doc.getElementsByTagName("*");

        int k =0;
        for (int i=0; i<nodeList.getLength(); i++) {

            Node node = (Node)nodeList.item(i);

            if(node.getNodeName().trim().equalsIgnoreCase(tags[k])){
                node.setTextContent(tagValues[k]);
                k++;
            }
        }

        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        LSSerializer lsSerializer = domImplementation.createLSSerializer();
        return lsSerializer.writeToString(doc);
    }

    /**
     * @param args
     * @throws IOException 
     * @throws JDOMException 
     */
    public static String jdom(File file, String[] tags, String[] tagValues) throws JDOMException, IOException {
        SAXBuilder saxfac = new SAXBuilder();
        Document doc = saxfac.build(file);

        Iterator<?> it = doc.getDescendants(new ElementFilter());
        int k =0;
        while (it.hasNext()) {

            Element emt = (Element)it.next();

            if(emt.getName().equalsIgnoreCase(tags[k])){
                emt.setText(tagValues[k]);
                k++;
            }
        }

        XMLOutputter xout = new XMLOutputter();
        return xout.outputString(doc);
    }
}