我编写了一个 Swing GUI,它是一个基于文本区域的 GUI。当用户将数据输入 GUI 时,它应该能够在适当的位置将这些数据合并到单独的 XML 模板文件中。我怎样才能做到这一点?
请让我知道步骤以及用于执行此操作的开源工具。
I used javax.xml to build a class to generate the XML from a Document
.
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);
}
}
Then, in each of my model classes, I had a toXMLString
method.
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;
}