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 从 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)          
}  

方法 process(word) 是这样的:

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

问题是我逐行阅读它..

4

1 回答 1

0

Extract the text form the file first. and then you can create xml document with tags as below using the W3C DOM. Modify the code accordingly.

http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/. Example of how to create a xml file.

 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 回答