Java XML String Transform createXml(ArrayList nameList, ArrayList stringList, ArrayList stringArrayNameList, HashMap> stringArrayContentMap)

Here you can find the source of createXml(ArrayList nameList, ArrayList stringList, ArrayList stringArrayNameList, HashMap> stringArrayContentMap)

Description

create Xml

License

Apache License

Declaration

public static void createXml(ArrayList<String> nameList, ArrayList<String> stringList,
            ArrayList<String> stringArrayNameList, HashMap<Integer, ArrayList<String>> stringArrayContentMap) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

public class Main {
    public static void createXml(ArrayList<String> nameList, ArrayList<String> stringList,
            ArrayList<String> stringArrayNameList, HashMap<Integer, ArrayList<String>> stringArrayContentMap) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {/*from  w  ww .  j  a v  a2  s.co m*/
            builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            Element mainRootElement = document
                    .createElementNS("xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\"", "resources");
            document.appendChild(mainRootElement);
            for (int t = 0; t < nameList.size(); t++) {
                mainRootElement.appendChild(getNode(document, "string", nameList.get(t), stringList.get(t)));
            }

            for (int i = 0; i < stringArrayNameList.size(); i++) {
                Node stringArrayNode = getNode(document, "string-array", stringArrayNameList.get(i), null);
                for (int j = 0; j < stringArrayContentMap.get(i).size(); j++) {
                    stringArrayNode
                            .appendChild(getNode(document, "item", null, stringArrayContentMap.get(i).get(j)));
                }
                mainRootElement.appendChild(stringArrayNode);
            }

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            // transformer.setOutputProperty(name, value);
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new File("out.xml"));
            transformer.transform(source, result);
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static Node getNode(Document document, String nodeName, String attrName, String value) {
        Element node = document.createElement(nodeName);
        if (attrName != null) {
            node.setAttribute("name", attrName);
        }
        node.setTextContent(value);
        return node;
    }
}

Related

  1. createOrUpdateConnection(String fileName, String directory, String communityId, String serviceName, String serviceUrl, String defaultVersion, Logger log)
  2. createSettingsXml(Map props)
  3. createSource(final String message)
  4. createSource(String msg)
  5. createString(Element element)
  6. createXmlError(String message, String details)
  7. createXmlEventReaderOnXmlString(String xml, String docBaseUri)
  8. decodeHex(String hex)
  9. elementToXMLString(Object aElement)