0

我有没有固定格式的 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 做同样的事情。

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

4

1 回答 1

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);
    }
}
于 2012-06-11T12:27:58.467 回答