2
這裏的情況,我已經下載從服務器的文本文件,它看起來像這樣:如何將文本文件轉換爲xml文件?
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從sdcard和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)
}
的方法處理(字)是這樣的:
TransformerHandler日; AttributesImpl atts;
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();
}
的問題是,我一行行讀吧..
你能告訴我們一些你已經嘗試過的代碼嗎? – rhughes 2013-03-23 07:28:41
我編輯了我的帖子,請看這些方法。 – 2013-03-23 07:49:42
在公共void proces(String s),有沒有辦法分割這些行?並把它放到每個標籤上,在上面的例子中我測試了兩個標籤,分別是<1stHome>和
。 – 2013-03-23 07:54:20