如何将Swing GUI中的数据合并到XML模板文件中?

时间:2012-04-10 14:50:06

标签: java xml eclipse swing templates

我编写了一个基于文本区域的GUI的Swing GUI。当用户将数据输入GUI时,它应该能够将这些数据合并到适当位置的单独XML模板文件中。我怎样才能做到这一点?

请让我知道步骤以及用于执行此操作的开源工具。

1 个答案:

答案 0 :(得分:3)

我使用javax.xml构建一个类来从Document生成XML。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.Document;
import org.w3c.dom.Element;

import com.ggl.mapping.model.WorkMap;

public class GenerateXML {

    public static final String DISPLAY_DEGREES = "display_degrees";

    WorkMap workMap;

    public GenerateXML(WorkMap workMap) {
        this.workMap = workMap;
    }

    public void execute() throws ParserConfigurationException,
            TransformerConfigurationException, TransformerException,
            TransformerFactoryConfigurationError, UnsupportedEncodingException, 
            FileNotFoundException {
        DocumentBuilderFactory dbInstance = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder = dbInstance.newDocumentBuilder();
        Document document = builder.newDocument();

        Element layout = document.createElement("layout");
        layout.setAttribute(DISPLAY_DEGREES,
                new Boolean(workMap.isDisplayDegrees()).toString());
        document.appendChild(layout);

        workMap.toXMLString(document, layout);

        TransformerFactory tfInstance = TransformerFactory.newInstance();
        Transformer transformer = tfInstance.newTransformer();
        DOMSource source = new DOMSource(document);
        String fileName = workMap.getFullSaveFileName();
        StreamResult result = new StreamResult(new OutputStreamWriter(
                new FileOutputStream(fileName), "UTF-8"));
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(source, result);
    }

}

然后,在我的每个模型类中,我都有一个toXMLString方法。

public void toXMLString(Document document, Element layout) {
    for (EarthCoordinate earthCoordinate : earthCoordinateList) {
        Element coordinate = earthCoordinate.toXMLString(document);
        layout.appendChild(coordinate);
    }
}

public Element toXMLString(Document document) {
    Element coordinateElement = document.createElement(COORDINATE);
    coordinateElement.setAttribute(LATITUDE, 
            getValueDegrees(getLatitude()));
    coordinateElement.setAttribute(LONGITUDE,
            getValueDegrees(getLongitude()));
    coordinateElement.setAttribute(TRACK_COORDINATE, new Boolean(
            isTrackCoordinate()).toString());

    if (!getDescription().equals("")) {
        Element descriptionElement = document.createElement(DESCRIPTION);
        descriptionElement.setTextContent(getDescription());
        coordinateElement.appendChild(descriptionElement);
    }

    return coordinateElement;
}