[英]store === characters in database for empty data while processing xml file
[英]Error while processing xml file
我的要求是處理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(系統找不到指定的路徑)。
有什么辦法嗎?
我創建了此類以從保存在內部存儲設備中的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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.