dynamically set value to xml using jdom

对着背影说爱祢 提交于 2020-01-16 18:07:08

问题


I have XML that have no fixed format I have following code using org.w3c.dom to dynamically set value to same 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;
    }

But its not working in some older version of JDK so i want to do the same with JDOM.

How is it possible ? Each example needs tag name but i want create common method.


回答1:


As far as I can tell, offhand, these two methods will do 'the same thing':

I am using JDOM 1.x since you want to use an 'old' JDK (I assume pre-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);
    }
}


来源:https://stackoverflow.com/questions/10974766/dynamically-set-value-to-xml-using-jdom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!