用jquery读取xml的时候出现这样的问题。
xml中没有中文的时候没有问题, 只要数据是中文的就会失败,请教下为什么?
代码:
<?xml version="1.0" encoding="UTF-8"?>
test测试
222333333333
js:
$.ajax({
ContentType: "text/xml;UTF-8",
url:'secondclass1.xml',
type:'GET',
dataType:"xml",
timeout:1000,
error:function(xml){alert('Error Loading XML document' + xml);},
success:function(xml){
alert(xml);
$(xml).find("aaa").each(function(i){
var id = $(this).children("id"); //取对象
var id_value = id.text(); //取文本 或者 $("id",xml).text();
alert(id_value);
var name = $(this).children("name"); //取对象
var name_value = name.text(); //取文本 或者 $("id",xml).text();
alert(name_value);
});
}
});
[b]问题补充:[/b]
试了, 还是不行。
不过我用其他方法又遇到了其他的问题, 看你能解决不。
我用别的方法去读xml,用jquery去解析。
$(oxml).find("secondclass > class").each(function(){
alert("111");
});
secondclass 是根目录,class是二级。
这一句死活不执行, 我alert(oxml);显示[Object]
请教
[b]问题补充:[/b]
是的;IE7
[b]问题补充:[/b]
多谢,这一步已经按你的提示完成,
但又遇到个问题,
我的xml文件是动态用dom4j来生成的,如果用记事本另存为utf-8格式的,是没有问题的。
但我不知道dom4j怎么去设置文本本身的格式,所以到这一步的时候无法执行。
$(xml).find("aaa").each(function(i){
var id = $(this).children("id"); //取对象
var id_value = id.text(); //取文本 或者 $("id",xml).text();
alert(id_value);
var name = $(this).children("name"); //取对象
var name_value = name.text(); //取文本 或者 $("id",xml).text();
alert(name_value);
});
不会去遍历,有解决方法吗?谢谢
[b]问题补充:[/b]
谢谢,问题解决!
在用dom4j时,不应该用FileWriter对象来构建xml文档,而应该使用FileOutputStream对象来构建,这样生成的文本本身的编码是utf-8的!
http://www.blogjava.net/biiau/archive/2008/09/24/231005.html
http://www.dom4j.org/dom4j-1.6.1/cookbook.html
[color=red]ContentType: "text/xml;charset=UTF-8", [/color]
你用的是ie吗?
对于ie的情况加这部分处理更好
[code="java"]
$.ajax({
ContentType: "text/xml;UTF-8",
url:'secondclass1.xml',
type:'GET',
timeout:1000,
error:function(xml){alert('Error Loading XML document' + xml);},
[color=red]dataType: ($.browser.msie) ? "text" : "xml",
success: function(data){
var xml;
if (typeof data == "string") {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
}[/color]
alert(xml);
$(xml).find("aaa").each(function(i){
var id = $(this).children("id"); //取对象
var id_value = id.text(); //取文本 或者 $("id",xml).text();
alert(id_value);
var name = $(this).children("name"); //取对象
var name_value = name.text(); //取文本 或者 $("id",xml).text();
alert(name_value);
});
}
});
[/code]
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
public class DeployFileCreator {
private Document doc;
public void serializetoXML(OutputStream out, String aEncodingScheme) throws Exception {
OutputFormat outformat = OutputFormat.createPrettyPrint();
//设置文本的编码
outformat.setEncoding(aEncodingScheme);
XMLWriter writer = new XMLWriter(out, outformat);
writer.write(this.doc);
writer.flush();
}
}
看看下面文章就行了
http://www.blogjava.net/junglesong/archive/2008/02/21/181196.html