html文件:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src="jquery-1.7.min.js"></script>
<script src="JSLoadXML.js"></script>
<script type="text/javascript">
$(function () {
var xmlFile = 'class.xml';
var xmlDocument = load_xml_file(xmlFile);
// alert( xmlDocument); //错误! 异步处理了同一js对象了
$.ajax({
type: "post", // 只能使用post发送xml节点,不能使用get
url: "test.php",
processData: false,
data: xmlDocument, //只能发送xml文档节点,别的节点不能成功发送
success: function (res) {
alert(res);
$("#pid").html(res);
}
});
});
</script>
</head>
<body>
<p id="pid">aaaaaaaaaaaaaaa</p>
</body>
</html>
class.xml:
<?xml version="1.0" encoding="utf-8"?>
<class><!-- 注释 -->
<student>
<name>孙悟空</name>
<sex>男</sex>
</student>
</class>
服务器文件:
<?php
header('content-type:text/html;charset=utf-8');
$con = file_get_contents("php://input");
$fp= fopen("a.xml","w");
fwrite ( $fp, $con );
fclose( $fp );
$doc = new DOMDocument('1.0','UTF-8');
$doc->loadXML($con);
//$str=$doc->saveXML();
//echo $str;
$doc->save('b.xml');
echo $doc->getElementsByTagName('name')->item(0)->nodeValue;
?>