qq_43308851
precious。。。
采纳率0%
2018-10-16 13:37

请问下面的报错是什么原因,试过new xmlwriter的时候去掉format,能成功写入xml文件,但是没有格式?

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:687)
at org.dom4j.io.XMLWriter.writeElementContent(XMLWriter.java:1074)
at org.dom4j.io.XMLWriter.writeElement(XMLWriter.java:954)
at org.dom4j.io.XMLWriter.writeNode(XMLWriter.java:1301)
at org.dom4j.io.XMLWriter.writeElementContent(XMLWriter.java:1086)
at org.dom4j.io.XMLWriter.writeElement(XMLWriter.java:954)
at org.dom4j.io.XMLWriter.writeNode(XMLWriter.java:1301)
at org.dom4j.io.XMLWriter.write(XMLWriter.java:344)
at sjzxy.lxy.jyj.xml.DOM4jUtils.transform(DOM4jUtils.java:35)
at sjzxy.lxy.jyj.xml.CRUD_xml.add(CRUD_xml.java:29)
at sjzxy.lxy.jyj.xml.CRUD_xmlTest.main(CRUD_xmlTest.java:14)

操作系统:win10
myeclipse 10
dom4j1.6.1

package sjzxy.lxy.jyj.xml;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class DOM4jUtils {
// 获取document对象
public static Document getDocum(String path) throws DocumentException {
// 获取解析器
SAXReader reader = new SAXReader();
// 获取document对象
return reader.read(path);
}

// 回写
public static void transform(String path, Document document) {
    // 回写

    // 漂亮的格式
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    try {
        XMLWriter writer = new XMLWriter(new FileWriter(path),format);
        writer.write(document);

        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

 package sjzxy.lxy.jyj.xml;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

public class CRUD_xml {
    public void add(Student stu) throws Exception{
        String path = "src/students.xml";
        //获取根节点
        Document document = DOM4jUtils.getDocum(path);
        Element root = document.getRootElement();



        //添加结点
        root.addElement("student").setText("");

        //获取student结点
        List<Element> list = root.elements();
        //System.out.println(list.get(0).getText());
        Element student = list.get(list.size()-1);

        student.addElement("name").setText(stu.getName());
        student.addElement("num").setText(stu.getNum());
        student.addElement("desc").setText(stu.getDesc());
        //回写
        DOM4jUtils.transform(path, document);
    }
}


package sjzxy.lxy.jyj.xml;

public class CRUD_xmlTest {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Student stu = new Student("1111","jason","好学生");
    CRUD_xml xm = new CRUD_xml();
    try {
        xm.add(stu);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • sinat_38745588 sinat_38745588 6月前
        OutputFormat format = OutputFormat.createPrettyPrint();
        //如下 设置便可,具体解释参考dom4j的API
        format.setPadText(false);
    
    点赞 评论 复制链接分享

相关推荐