IE8浏览器显示如下:
无法显示 XML 页。
使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。
--------------------------------------------------------------------------------XML 文档只能有一个顶层元素。处理资源 'http://localhost/xmlStudy/testxx.php' 时出错。第 3 行,位置: 2 <pre>Array
-^
test.php
<?php
require_once 'xml.class.php';
$cardId = '0001';
$cardPassword = 'psw';
$paymentMoney = '$10000';
$commandType = 'PAYMENT';
//实例化
$xml = new xml ();
//创建xml
$xmlStr = $xml->creatXml ( "root",
array ("root/bank/login/card-id\\valuestart" . $cardId,
"root/bank/login/card-password\\valuestart" . $cardPassword,
"root/bank/command/money\\valuestart" . $paymentMoney, ),
array (array ("command", "type|" . $commandType ) ) );
//输出xml
echo $xmlStr;
//解析xml
$xml->parseString($xmlStr);
$parseArr = $xml->getTree();
//输出数组
echo "<pre>";
print_r($parseArr);
echo "<pre>";
?>创建和解析应该没有问题,因为同分别输出的时候可以正确显示!但我想同时输出xml文件和解析后的数组!!谢谢帮帮我吧!
无法显示 XML 页。
使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。
--------------------------------------------------------------------------------XML 文档只能有一个顶层元素。处理资源 'http://localhost/xmlStudy/testxx.php' 时出错。第 3 行,位置: 2 <pre>Array
-^
test.php
<?php
require_once 'xml.class.php';
$cardId = '0001';
$cardPassword = 'psw';
$paymentMoney = '$10000';
$commandType = 'PAYMENT';
//实例化
$xml = new xml ();
//创建xml
$xmlStr = $xml->creatXml ( "root",
array ("root/bank/login/card-id\\valuestart" . $cardId,
"root/bank/login/card-password\\valuestart" . $cardPassword,
"root/bank/command/money\\valuestart" . $paymentMoney, ),
array (array ("command", "type|" . $commandType ) ) );
//输出xml
echo $xmlStr;
//解析xml
$xml->parseString($xmlStr);
$parseArr = $xml->getTree();
//输出数组
echo "<pre>";
print_r($parseArr);
echo "<pre>";
?>创建和解析应该没有问题,因为同分别输出的时候可以正确显示!但我想同时输出xml文件和解析后的数组!!谢谢帮帮我吧!
<?php
/**
* @package transaction
* @desc xml处理类
* @copyright
* @see
* @author wj
* @version 1.0
*/
class xml {
private $xmlStr = '';
//xml文件
private $file = '';
/**
* the original struct
*
* @access private
* @var array
*/
var $_struct = array ();
/**
*构造函数
*/
public function __construct($file = '') {
if ($file != '') {
$this->file = $file;
}
}
/**
*析构函数
*/
function __destruct() {
}
/**
* 获取file
* @return $str xml文件路径
* @access public
*/
function getFilePath() {
return $this->file;
}
/**
* 创建xml
* @param string 根节点
* @param array 路径数组 e.g. array("eweb/bank/login/account-id\\valuestart3333"\\valuestart...)
* @param array $att_arr 属性数组 e.g. array(arrar(a\\valuestarta_att|a_att_value),...)
* @return $str XML流
* @access public
*/
function creatXml($rootNode, $pathArr = array(), $attArr = array()) {
$dom = new DOMDocument ( "1.0", "gbk" );
header ( "Content-Type: text/plain" );
//生成根节点
$root = $dom->createElement ( $rootNode );
$dom->appendChild ( $root );
//遍历路径数组,调用addNode()方法,生成子节点
foreach ( $pathArr as $key => $value ) {
$temp = array ();
//分割路径和节点的值
$temp = explode ( "\\valuestart", $value );
$this->addNode ( $dom, $temp [0], $attArr, $temp [1] );
}
$xmlStr = $dom->saveXML ();
// $fileName = creatXmlName();
// $dom->save($fileName);
return $xmlStr;
}
/**
* Parses the XML file
*
* @access public
* @param string [$file] the XML file name
* @return void
* @since
*/
function parseFile($file) {
$data = @file_get_contents ( $file ) or die ( "Can't open file $file for reading!" );
$this->parseString ( $data );
}
/**
* Parses a string.
*
* @access public
* @param string [$data] XML data
* @return void
*/
function parseString($data) {
if ($this->srcenc === null) {
$this->parser = @xml_parser_create () or die ( 'Unable to create XML parser resource.' );
} else {
$this->parser = @xml_parser_create ( $this->srcenc ) or die ( 'Unable to create XML parser resource with ' . $this->srcenc . ' encoding.' );
}
if ($this->dstenc !== null) {
@xml_parser_set_option ( $this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc ) or die ( 'Invalid target encoding' );
}
xml_parser_set_option ( $this->parser, XML_OPTION_CASE_FOLDING, 0 ); // lowercase tags
xml_parser_set_option ( $this->parser, XML_OPTION_SKIP_WHITE, 1 ); // skip empty tags
if (! xml_parse_into_struct ( $this->parser, $data, &$this->_struct )) {
printf ( "XML error: %s at line %d", xml_error_string ( xml_get_error_code ( $this->parser ) ), xml_get_current_line_number ( $this->parser ) );
$this->free ();
exit ();
}
$this->_count = count ( $this->_struct );
$this->free ();
}
/**
* return the data struction
*
* @access public
* @return array
*/
function getTree() {
if ($this->file != '') {
$this->parseFile ( $this->file );
}
$i = 0;
$tree = array ();
$tree = $this->addInfo ( $tree, $this->_struct [$i] ['tag'], (isset ( $this->_struct [$i] ['value'] )) ? $this->_struct [$i] ['value'] : '', (isset ( $this->_struct [$i] ['attributes'] )) ? $this->_struct [$i] ['attributes'] : '', $this->getChild ( $i ) );
unset ( $this->_struct );
return ($tree);
}
/**
* recursion the children node data
*
* @access public
* @param integer [$i] the last struct index
* @return array
*/
function getChild(&$i) {
// contain node data
$children = array ();
// loop
while ( ++ $i < $this->_count ) {
// node tag name
$tagname = $this->_struct [$i] ['tag'];
$value = isset ( $this->_struct [$i] ['value'] ) ? $this->_struct [$i] ['value'] : '';
$attributes = isset ( $this->_struct [$i] ['attributes'] ) ? $this->_struct [$i] ['attributes'] : '';
switch ($this->_struct [$i] ['type']) {
case 'open' :
// node has more children
$child = $this->getChild ( $i );
// append the children data to the current node
$children = $this->addInfo ( $children, $tagname, $value, $attributes, $child );
break;
case 'complete' :
// at end of current branch
$children = $this->addInfo ( $children, $tagname, $value, $attributes );
break;
case 'cdata' :
// node has CDATA after one of it's children
$children ['value'] .= $value;
break;
case 'close' :
// end of node, return collected data
return $children;
break;
}
}
//return $children;
}
/**
* Appends some values to an array
*
* @access public
* @param array [$target]
* @param string [$key]
* @param string [$value]
* @param array [$attributes]
* @param array [$inner] the children
* @return void
* @since
*/
function addInfo($target, $key, $value = '', $attributes = '', $child = '') {
if (! isset ( $target [$key] ['value'] ) && ! isset ( $target [$key] [0] )) {
if ($child != '') {
$target [$key] = $child;
}
if ($attributes != '') {
foreach ( $attributes as $k => $v ) {
$target [$key] [$k] = $v;
}
}
$target [$key] ['value'] = $value;
} else {
if (! isset ( $target [$key] [0] )) {
// is string or other
$oldvalue = $target [$key];
$target [$key] = array ();
$target [$key] [0] = $oldvalue;
$index = 1;
} else {
// is array
$index = count ( $target [$key] );
}
if ($child != '') {
$target [$key] [$index] = $child;
}
if ($attributes != '') {
foreach ( $attributes as $k => $v ) {
$target [$key] [$index] [$k] = $v;
}
}
$target [$key] [$index] ['value'] = $value;
}
return $target;
}
/**
* Free the resources
*
* @access public
* @return void
**/
function free() {
if (isset ( $this->parser ) && is_resource ( $this->parser )) {
xml_parser_free ( $this->parser );
unset ( $this->parser );
}
}
/**
* 为已存在节点添加属性,值
* @param string xml文件
* @param string 节点路径
* @param array 节点属性数组 e.g array("att1|att1_value","att2|att2_value",...)
* @param string 节点值
* @access public
* @return void
**/
function addAttValue($path, $att_arr = array(), $nodeValue) {
$dom = new DOMDocument ( "1.0", "gbk" );
$dom->load ( $this->file );
$xPath = new DOMXPath ( $dom );
//确定节点位置
$nodeList = $xPath->query ( $path );
$thisNode = $nodeList->item ( 0 );
foreach ( $att_arr as $key => $value ) {
//分开属性名称和属性的值
list ( $att_name, $att_value ) = split ( "[|]", $value );
//添加属性
$att = $dom->createAttribute ( $att_name );
$thisNode->appendChild ( $att );
$text = $dom->createTextNode ( $att_value );
$att->appendChild ( $text );
}
//添加节点的值
$text = $dom->createTextNode ( $nodeValue );
$thisNode->appendChild ( $text );
//保存
$dom->save ( $this->file );
}
/**
* 添加节点
* @param string xml文件
* @param string 节点路径
* @param array 属性数组
* @param string 节点值
* @access public
* @return void
**/
function addNode($dom, $path, $att_arr = array(), $nodeValue = '') {
//为路径字符串添加"/"
if (substr ( $path, 0, 1 ) != '/') {
$path = '/' . $path;
}
//准备工作
$path_arr = $this->explodeParth ( $path );
$xPath = new DOMXPath ( $dom );
//添加节点
foreach ( $path_arr as $key => $pathStr ) {
if ($xPath->query ( $pathStr )->length > 0) {
continue;
} else {
//获取节点
$tempArr = explode ( "/", $pathStr );
$count = count ( $tempArr );
$currentNode = $dom->getElementsByTagName ( $tempArr [$count - 2] )->item ( 0 );
$newEle = $dom->createElement ( $tempArr [$count - 1] );
$currentNode->appendChild ( $newEle );
}
}
//属性
foreach ( $att_arr as $key => $value ) {
$node = $dom->getElementsByTagName ( $value [0] )->item ( 0 );
if ($node == '') {
continue;
}
//分开属性名称和属性的值
list ( $att_name, $att_value ) = split ( "[|]", $value [1] );
//添加属性
$att = $dom->createAttribute ( $att_name );
$node->appendChild ( $att );
$text = $dom->createTextNode ( $att_value );
$att->appendChild ( $text );
}
//赋值
$currentNode = $xPath->query ( $path )->item ( 0 );
$text = $dom->createTextNode ( $nodeValue );
$currentNode->appendChild ( $text );
//保存
$xmlStr = $dom->saveXML ();
}
/**
* 分割路径
* @param string 节点路径
* @access public
* @return array
**/
private function explodeParth($url) {
//用"/"分割路径,存在数组$part中
$part = array ();
$part = explode ( "/", $url );
$coutPart = count ( $part );
$tempArr = array ();
$parthArr = array ();
for($i = 1; $i < $coutPart; $i ++) {
//累加地址存放在数组$tempArr中
$tempArr [] = '/' . $part [$i];
//把二维数组转化成一维数组
$parthArr [$i - 1] = implode ( '', $tempArr );
}
return $parthArr;
}}?>
echo $xmlStr;
//解析xml
$xml->parseString($xmlStr);
$parseArr = $xml->getTree();
//输出数组
echo "<pre>";
print_r($parseArr);
echo "<pre>";只能输出一样,输出了xml内容就不要输出其他的,否则浏览器解析的时候会出错.