因为XML文档中有中文字符,是否是因为编码不对造成的呢?该如何解决呢?请教!public void createXMLDocument(){
Document doc = null;
Element people=null,person=null,name=null,age=null,sex=null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument(); if(doc != null){
people = doc.createElement("People"); person = doc.createElement("Person");
person.setAttribute("id","1");
people.appendChild(person); name = doc.createElement("Name");
name.appendChild(doc.createTextNode("张三"));
person.appendChild(name); age = doc.createElement("Age");
age.appendChild(doc.createTextNode("20"));
person.appendChild(age); sex = doc.createElement("Sex");
sex.appendChild(doc.createTextNode("男"));
person.appendChild(sex); doc.appendChild(people); ((XmlDocument)doc).write(System.out);
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
Document doc = null;
Element people=null,person=null,name=null,age=null,sex=null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument(); if(doc != null){
people = doc.createElement("People"); person = doc.createElement("Person");
person.setAttribute("id","1");
people.appendChild(person); name = doc.createElement("Name");
name.appendChild(doc.createTextNode("张三"));
person.appendChild(name); age = doc.createElement("Age");
age.appendChild(doc.createTextNode("20"));
person.appendChild(age); sex = doc.createElement("Sex");
sex.appendChild(doc.createTextNode("男"));
person.appendChild(sex); doc.appendChild(people); ((XmlDocument)doc).write(System.out);
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
这个头难道不写吗?