2

これが状況です。サーバーからテキストファイルをダウンロードしました。次のようになります。

Home  
Address  
Suburb  
State   
Post Code       
Latitude    
Longitude   
Phone    
Fax   
Curfew Hours Start  
Curfew Hours End  
Website

FirstHome Address   
"123 Street sd" 
FirstHome Address   
HMH  
2223 "Addresss,dsdsd"   
-54.000012  
120.000000  
(03) 1232 1242    
(03) 1232 3244  
Mon-Sun 10pm    
"Mon-Sun 6am"   
http:www.dsdsdsfirsthome.com

2ndHome     
2903 Building 1     
2ndHome         
2HMF     
3875    "2nd Adddedere" 
-00.00001   
002.323232  
(03) 2223 2323  
(03) 1233 4343      
http:dsdd

asdsfadf.com

そして、次のような XML ファイルに変換する必要があります。

ここに画像の説明を入力

何か案は?前もって感謝します。

BufferedReader を使用して SD カードからテキスト ファイルを読み取り、StreamResult を使用して XML ファイルを書き込みました。そして、これを実行しました:

TransformerConfigurationException, SAXException {      
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();     
th = tf.newTransformerHandler();   
Transformer serializer = th.getTransformer();     
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");       serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");  serializer.setOutputProperty(OutputKeys.INDENT, "yes");        
th.setResult(aStreamResult);    
th.startDocument();     
atts = new AttributesImpl();    
th.startElement("", "", "Homes", atts);

その後、while ループが呼び出されます。

while ((aString = aBufferedReader.readLine()) != null){          
process(word)          
}  

メソッド process(word) は次のとおりです。

TransformerHandler 番目; AttributesImpl 属性;

public void process(String s) throws SAXException {         
String[] elements = s.split(" ");           
atts.clear();         
th.startElement("", "", "Home", atts);          
th.startElement("", "", "1stHome", atts);          
th.characters(elements[0].toCharArray(), 0, elements[0].length());         
th.endElement("", "", "1stHome");              
th.startElement("", "", "Address", atts);          
 th.characters(elements[0].toCharArray(), 0, elements[0].length());           
th.endElement("", "", "Address");          
th.endElement("", "", "Home");               
}

その後、closeXML(); を呼び出してタグを閉じます。

public void closeXML() throws SAXException {            
th.endElement("", "", "Homes");          
th.endDocument();         
}

問題は、1行ずつ読むことです..

4

1 に答える 1

0

最初にファイルからテキストを抽出します。そして、W3C DOM を使用して、以下のようなタグで xml ドキュメントを作成できます。それに応じてコードを変更します。

http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/。xml ファイルの作成方法の例。

 public class modifyXML {

   public modifyXML()
{
 try {


DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
Document doc = docBuilder.newDocument();
Element question = doc.createElement("question");
doc.appendChild(rootElement);

    Node  option= doc.createElement("option1");
    option.setTextContent("option1");
    question.appendChild(option);

//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();

    //create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);
    String xmlString = sw.toString();

    OutputStream f0;
byte buf[] = xmlString.getBytes();
f0 = new FileOutputStream("pathofxmlfile"+filename);
for(int i=0;i<buf .length;i++) {
   f0.write(buf[i]);
}
f0.close();
buf = null;
 }
 catch(SAXException e) {
e.printStackTrace();
 }
 catch(IOException e) {
    e.printStackTrace();
 }
 catch(ParserConfigurationException e) {
   e.printStackTrace();
 }
 catch(TransformerConfigurationException e) {
   e.printStackTrace();
 }
 catch(TransformerException e) {
   e.printStackTrace();
 }

}
}
于 2013-03-23T07:33:30.893 に答える