日期:2014-05-20  浏览次数:20843 次

java读写xml的问题
我用javax.xml等等java自带的包去读写xml,读出来都没什么问题。可是写就有问题了
本来我的xml文件是
<IIsWebServer Location ="/LM/W3SVC/1"
AppPoolId="DefaultAppPool"
DefaultDoc="Default.htm,Default.asp,index.htm,iisstart.htm"
ServerBindings=":80:
  192.168.0.1"
ServerComment="Default Web Site"
ServerSize="1"
>
</IIsWebServer>
结果在写到文件里面都成了

<IIsWebServer AppPoolId="DefaultAppPool" Location "/LM/W3SVC/1"DefaultDoc="Default.htm,Default.asp,index.htm,iisstart.htm"
ServerBindings=":80:" ServerComment="Default Web Site" ServerSize="1"
>
</IIsWebServer>
该换行的都不换行了,而且属性的位置也不一致了(这个倒无所谓),主要是
ServerBindings=":80:
  192.168.0.1"
这样的数据,没有换行我的iis服务就不认识了。不知道怎么才能让""里面的内容换行呢?


------解决方案--------------------
// 以下设置输出的自动换行和缩进
transformer .setOutputProperty(OutputKeys.INDENT, "yes");

------解决方案--------------------
try
{
String context="";
String str="";
File file=new File("f:\\job.txt");// 文件名你自己设
FileReader fReader=new FileReader(file);
BufferedReader bReader=new BufferedReader(fReader);
while((str=bReader.readLine())!=null)
{
context=context+str+"\r\n";
}
bReader.close();
fReader.close();
File file1=new File("f:\\jobCopy.txt"); //新写成的文件名你自己设
FileWriter fWriter=new FileWriter(file1);
BufferedWriter bWriter=new BufferedWriter(fWriter);
bWriter.write(context);

bWriter.close();
fWriter.close();










}

catch(IOException e)
{
System.out.println("IO Failed!");
}
------解决方案--------------------
建议用dom4j去读写xml文件
------解决方案--------------------
用JDOM 
SAXBuilder builder = new SAXBuilder();
Document Doc = builder.build("D://temp/10.xml");
XMLOutputter XMLOut = new XMLOutputter(); 
String ss = XMLOut.outputString(Doc);

public static String createFile(String ss){
String strUrl = null;
try{
File f = new File("D:\temp");
File file = File.createTempFile("temp", ".xml", f);
if(file.exists()){
strUrl =file.getCanonicalPath();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(ss);
writer.close();
}catch(IOException e){
e.printStackTrace();
}
return strUrl;
}
------解决方案--------------------
可以用xsl进行设置格式
------解决方案--------------------
换种思路,可否对
ServerBindings=":80: 
192.168.0.1"
进行一下处理
------解决方案--------------------

换种思路,可否对
ServerBindings=":80: 
192.168.0.1"
进行一下处理
------解决方案--------------------
Java code


package dom4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ReadXml {
    /**
     * @param args
     */
    XMLWriter output = null;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ReadXml rx = new ReadXml();
        SAXReader reader = new SAXReader();
        Document doc = null;
        
        try {
            doc= reader.read("test.xml");
            Element root = doc.getRootElement();
            rx.addXml(root);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
        
    public void addXml(Element elementroot){
        Document document = DocumentHelper.createDocument();
        XMLWriter writer = null;
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        format.setExpandEmptyElements(true);
        try {
            Element root = document.addElement(elementroot.getName());
            Iterator iter = elementroot.attributeIterator();  
            while(iter.hasNext()){
                Attribute attr = (Attribute) iter.next();
                StringBuffer val = new StringBuffer(attr.getValue());
                int blank = val.indexOf(" "); 
                if(blank!=-1)
                {
                    String result = val.substring(0, blank) + "\n" + val.substring(blank, val.length());
                    root.addAttribute("\n"+attr.getName(),result);
                }
                else
                    root.addAttribute("\n"+attr.getName(),attr.getValue());        
            }        
            writer = new XMLWriter(new FileWriter( "outfile.xml" ),format);
            writer.write( document );
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}