2013-03-23 378 views
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文件:

enter image description here

任何想法?先谢谢你。

我使用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();   
} 

的问题是,我一行行读吧..

+0

你能告诉我们一些你已经尝试过的代码吗? – rhughes 2013-03-23 07:28:41

+0

我编辑了我的帖子,请看这些方法。 – 2013-03-23 07:49:42

+0

在公共void proces(String s),有没有办法分割这些行?并把它放到每个标签上,在上面的例子中我测试了两个标签,分别是<1stHome>和

。 – 2013-03-23 07:54:20

回答

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

} 
}