2012-04-10 7 views
-3

私はテキストエリアベースのGUIであるSwing GUIを書いています。ユーザーがGUIにデータを入力すると、これらのデータを適切な場所にある個別のXMLテンプレートファイルにマージする必要があります。どうすればこれを達成できますか?Swing GUIのデータをXMLテンプレートファイルにマージするには?

これを実行するために使用するステップとオープンソースツールを教えてください。

+0

私はGoogleのClosure Tools、Jamon APIを使用していますが、この目的には役立ちます。 –

答えて

3

私はDocumentからXMLを生成するクラスを構築するためにjavax.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; 
} 
関連する問題