一个简易的PHP XML 操作类SmallXmlDOM
<?php
class SmallXmlDOM {
/**
* @var DOMDocument
*/
private $_doc;
/**
* @var DOMElement
*/
private $_root;
function __construct($rootNodeName,$charset='utf-8'){
$this->_doc = new DOMDocument('1.0',$charset);
$this->_root = $this->_doc->createElement($rootNodeName);
$this->_doc->appendChild($this->_root);
}
function asXML(){
return $this->_doc->saveXML();
}
/**
* @return DOMElement
*/
function getDocumentRoot(){
return $this->_root;
}
private function createElement($name,$value=NULL){
$subElement = $this->_doc->createElement($name);
/* @var $subElement DOMElement */
if ($value === null)
return $subElement;
$valueElement = $this->_doc->createCDATASection($value);
$subElement->appendChild($valueElement);
return $subElement;
}
/**
* 创建并追加 子节点到父节点后,并返回 创建的子节点对象
*
* @param DOMElement $parentNode
* @param string $nodeName
* @param mixed $nodeValue
*
* @return DOMElement
*/
function addChild(DOMElement $parentNode,$nodeName,$nodeValue=NULL){
$subNode = $this->createElement($nodeName,$nodeValue);
$parentNode->appendChild($subNode);
return $subNode;
}
/**
* convert xml string to php array - useful to get a serializable value
*
* @param string $xmlstr
* @return array
*/
static function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return self::domnode_to_array($doc->documentElement);
}
/**
* DOM节点转字符串
*
* @param DOMElement $node
* @return array
*/
static function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = self::domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = empty($v) ? null : $v;
}
elseif($v) {
$output = (string) $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='@attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
}[本话题由 vb2005xu 于 2011-10-24 18:41:47 编辑]
class SmallXmlDOM {
/**
* @var DOMDocument
*/
private $_doc;
/**
* @var DOMElement
*/
private $_root;
function __construct($rootNodeName,$charset='utf-8'){
$this->_doc = new DOMDocument('1.0',$charset);
$this->_root = $this->_doc->createElement($rootNodeName);
$this->_doc->appendChild($this->_root);
}
function asXML(){
return $this->_doc->saveXML();
}
/**
* @return DOMElement
*/
function getDocumentRoot(){
return $this->_root;
}
private function createElement($name,$value=NULL){
$subElement = $this->_doc->createElement($name);
/* @var $subElement DOMElement */
if ($value === null)
return $subElement;
$valueElement = $this->_doc->createCDATASection($value);
$subElement->appendChild($valueElement);
return $subElement;
}
/**
* 创建并追加 子节点到父节点后,并返回 创建的子节点对象
*
* @param DOMElement $parentNode
* @param string $nodeName
* @param mixed $nodeValue
*
* @return DOMElement
*/
function addChild(DOMElement $parentNode,$nodeName,$nodeValue=NULL){
$subNode = $this->createElement($nodeName,$nodeValue);
$parentNode->appendChild($subNode);
return $subNode;
}
/**
* convert xml string to php array - useful to get a serializable value
*
* @param string $xmlstr
* @return array
*/
static function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return self::domnode_to_array($doc->documentElement);
}
/**
* DOM节点转字符串
*
* @param DOMElement $node
* @return array
*/
static function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = self::domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = empty($v) ? null : $v;
}
elseif($v) {
$output = (string) $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='@attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
}[本话题由 vb2005xu 于 2011-10-24 18:41:47 编辑]