Java 处理xml文件时出错

Java 处理xml文件时出错,java,xml,doctype,stringbuilder,Java,Xml,Doctype,Stringbuilder,我的要求是处理xml文件以搜索字符并替换它。 我正在为此使用以下代码: public static void main(String[] args) { try { StringBuilder sb = new StringBuilder(); File xmlFile = new File("C:/Users/demo.xml"); BufferedReader br = new BufferedReade

我的要求是处理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)


约瑟夫
并购
我收到错误:错误: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;
}

}
公共类readXMLFile{
私有字符串filePath=“FileStorage”;
私有字符串fileName=“File.xml”;
private final String tag=“内部读取持久化”;
文件内部文件数据;
public readXMLFile(){//默认构造函数
}
公共文件getXMLFile(上下文){
文件目录=null;
ContextWrapper cw=新的ContextWrapper(上下文);
directory=cw.getDir(filePath,Context.MODE\u PRIVATE);
internalFileData=新文件(目录、文件名);
if(internalFileData.exists()){
Log.i(“ReadXMLFile”,“返回的文件”);
返回内部文件数据;
}
否则{
i(标记“文件不存在!”);
返回null;
}
}
公共列表读取文件(上下文){
列表l=新的LinkedList();
试一试{
文件目录=null;
ContextWrapper cw=新的ContextWrapper(上下文);
directory=cw.getDir(filePath,Context.MODE\u PRIVATE);
internalFileData=新文件(目录、文件名);
if(internalFileData.exists()){
Log.i(“内部数据”,“根存在!!”;
试一试{
FileInputStream fis=新的FileInputStream(internalFileData);
DataInputStream in=新的DataInputStream(fis);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦线;
而((line=br.readLine())!=null){
l、 添加(行);
}
试一试{
if(in!=null){
in.close();
}
}捕获(例外e){
Log.i(标记“异常关闭持久性连接”);
}
}捕获(例外e){
wtf(“致命异常”,“异常:+e.getMessage());
}
}否则{
i(标记“文件不存在”);
return l;//返回空列表
}
}捕获(例外e){
wtf(标记,“异常数据读取:”+e.getMessage());
返回l;
}
Log.i(标记“文件找到返回”);
返回l;
}
}

将文件添加到路径或相应地更改它?明白了,doctype路径和源文件路径必须相同。。。谢谢
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;
}

}