2012-04-10 74 views
-3

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

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

+0

我已经通过谷歌的闭合工具,Jamon API的,但我发现它有用于此目的。 – 2012-04-10 14:51:17

回答

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; 
}