当前位置: 技术问答>java相关
jdom处理 xml时的中文问题??
来源: 互联网 发布时间:2015-04-26
本文导语: 我用如下代码: Element carElement = new Element("car"); Document myDocument = new Document(carElement); carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90")); carElement.addContent(new Element("make").addContent("中国"));...
我用如下代码:
Element carElement = new Element("car");
Document myDocument = new Document(carElement);
carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90"));
carElement.addContent(new Element("make").addContent("中国"));
carElement.addContent(new Element("model").addContent("Celica"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("year").addContent("1999"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license").addContent("1ABC234").setAttribute("state", "CA"));
carElement.addContent(new Comment("Description of a car"));
java.util.List yearElement = carElement.getChildren("year");
//Element fElement=new Element(yearElement.get(0).toString()) ;
Element fElement=(Element)yearElement.get(1);
System.out.println(fElement.getTextTrim());
yearElement.remove(1);
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.output(myDocument, System.out);
FileWriter writer = new FileWriter("d:/hym/java/myFile.xml");
outputter.output(myDocument, writer);
writer.close();
想生成一个有中文的xml
但中文的地方总是乱码?
涓浗
Celica
1997
green
1ABC234
而且还是 utf-8
怎么使它不是乱码,不是utf-8--->gb2312 ??
Element carElement = new Element("car");
Document myDocument = new Document(carElement);
carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90"));
carElement.addContent(new Element("make").addContent("中国"));
carElement.addContent(new Element("model").addContent("Celica"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("year").addContent("1999"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license").addContent("1ABC234").setAttribute("state", "CA"));
carElement.addContent(new Comment("Description of a car"));
java.util.List yearElement = carElement.getChildren("year");
//Element fElement=new Element(yearElement.get(0).toString()) ;
Element fElement=(Element)yearElement.get(1);
System.out.println(fElement.getTextTrim());
yearElement.remove(1);
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.output(myDocument, System.out);
FileWriter writer = new FileWriter("d:/hym/java/myFile.xml");
outputter.output(myDocument, writer);
writer.close();
想生成一个有中文的xml
但中文的地方总是乱码?
涓浗
Celica
1997
green
1ABC234
而且还是 utf-8
怎么使它不是乱码,不是utf-8--->gb2312 ??
|
org.jdom.output.XMLOutputter xop = new org.jdom.output.XMLOutputter(" ", true, "GB2312");
rslt = xop.outputString(returnDoc);
就可以了记着,给分。
rslt = xop.outputString(returnDoc);
就可以了记着,给分。