2013-07-26 40 views
0

我的要求是處理xml文件來搜索字符並將其替換。 我使用下面的代碼是:處理xml文件時出錯

public static void main(String[] args) { 
     try 
     { 
      StringBuilder sb = new StringBuilder(); 
     File xmlFile = new File("C:/Users/demo.xml"); 
     BufferedReader br = new BufferedReader(new FileReader(xmlFile)); 
      String line = null; 
      while((line = br.readLine())!= null){ 
      if(line.indexOf("&") != -1) 
      { 
       line = line.replaceAll("&","&"); 
      } 
       sb.append(line); 
      } 
       br.close(); 

       BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile)); 

       Source xmlInput = new StreamSource(new StringReader(sb.toString())); 
       StringWriter stringWriter = new StringWriter(); 
       StreamResult xmlOutput = new StreamResult(stringWriter); 
       TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
       transformerFactory.setAttribute("indent-number", 2); 
       Transformer transformer = transformerFactory.newTransformer(); 
       transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
       transformer.transform(xmlInput, xmlOutput); 


       bw.write(xmlOutput.getWriter().toString()); 
       bw.close(); 
       System.out.println("success"); 

      } 

     catch (Exception e) { 
      System.out.println("Error : " + e.getMessage()); 
     } 

當我的XML文件是:

<INFO> 
<NAME>Joseph</NAME> 
<BUSINESSNAME>M & A</BUSINESSNAME> 
<INFO> 

它給適當的輸出 但具有下列格式(實際的XML)

<!DOCTYPE CASE SYSTEM "C:\Program Files\abc.dtd"> 
<INFO> 
<NAME>Joseph</NAME> 
<BUSINESSNAME>M & A</BUSINESSNAME> 
<INFO> 

我收到錯誤:錯誤:java.io.FileNotFoundException:C:\ Program Files \ abc.dtd(系統找不到指定的路徑)。

任何解決方案?

+0

將文件添加到路徑或相應地更改它? – Lenymm

+0

明白了,doctype路徑和源文件路徑必須相同...謝謝 – Aquarius24

回答

0

我創建了這個類從保存在內部存儲設備中的xml文件讀取字符串,它返回一個列表,如果你想要整個擴展字符串只需要連接在一起鏈接在一起,如果沒有找到該文件返回一個空列表這是您需要讀取XML文件並解析爲字符串的所有內容,您可以使用列表來替換字符串來替換字母,我希望能夠提供幫助!

public class readXMLFile { 
private String filePath = "FileStorage"; 
private String fileName = "File.xml"; 
private final String tag = "Internal Read Persistence"; 
File internalFileData; 

public readXMLFile() {// default constructor 
} 

public File getXMLFile(Context context){ 
File directory = null; 
ContextWrapper cw = new ContextWrapper(context); 
directory = cw.getDir(filePath, Context.MODE_PRIVATE); 
internalFileData = new File(directory, fileName); 
if(internalFileData.exists()){ 
Log.i("ReadXMLFile","File returned"); 
return internalFileData; 
} 
else{ 
Log.i(tag,"the file doesn't exists!"); 
return null; 
} 
} 

public List<String> readFile(Context context) { 
List<String> l = new LinkedList<String>(); 
try { 
File directory = null; 
ContextWrapper cw = new ContextWrapper(context); 
directory = cw.getDir(filePath, Context.MODE_PRIVATE); 
internalFileData = new File(directory, fileName); 

if (internalFileData.exists()) { 
Log.i("Internal Data", "the root exists!!"); 
try { 
FileInputStream fis = new FileInputStream(internalFileData); 
DataInputStream in = new DataInputStream(fis); 
BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
String line; 
while ((line = br.readLine()) != null) { 
l.add(line); 
} 
try { 
if (in != null) { 
in.close(); 
} 
} catch (Exception e) { 
Log.i(tag, "Exception closing persistence connection"); 
      } 
} catch (Exception e) { 
Log.wtf("Fatal Exception", "Exception: " + e.getMessage()); 
} 
} else { 
Log.i(tag, "File doesn't exists"); 
return l;//return empty list 
} 
} catch (Exception e) { 
Log.wtf(tag, "Exception DATA READING: " + e.getMessage()); 
return l; 
} 
Log.i(tag, "file found return"); 
return l; 
} 

}