これが状況です。サーバーからテキストファイルをダウンロードしました。次のようになります。
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行ずつ読むことです..