0

我的要求是处理 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(系统找不到指定的路径)。

有什么解决办法吗?

4

1 回答 1

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

}
于 2013-12-26T17:45:31.470 回答