把全角空格替换成半角,然后把输出的地方改成你想进行的处理操作就可以了<?php class XMLAssistor { private $struct_str = NULL; private function loop($node) { // iterate all attributes of the current node $attrList = $node->attributes; foreach ($attrList as $childNode) { echo $childNode->nodeName . "[" . $childNode->nodeValue . "]<br>\n"; } // iterate all below elements $nodeList = $node->childNodes; foreach ($nodeList as $childNode) { if ($childNode->nodeType == XML_ELEMENT_NODE) { echo $childNode->nodeName . "[" . $this->getElementTextValue($childNode) . "]<br>\n"; $this->loop($childNode); } } } private function getElementTextValue($node) { $nodeList = $node->childNodes; foreach ($nodeList as $childNode) { if ($childNode->nodeType == XML_TEXT_NODE) { return $childNode->nodeValue; } } return ""; } private function checkForDom($dom) { if (!($dom instanceof DomDocument)) { die("Parameter \"dom\" is not an instance of DomDocument tyep."); } } /** * @desc show all elements of the given dom object */ public function showStruct($dom) { $root = $dom->documentElement; $this->loop($root); } /** * @desc return a list of nodes which match the given xpath expression. */ public function selectNodes($xpathExp, $dom) { $xpath = new DOMXPath($dom); $nodeList = $xpath->query($xpathExp); if ($nodeList->length == 1) { die("The given xpath expression is not valid"); } return $nodeList; } /** * @desc return a single node witch matches the given xpath expression. */ public function selectSingleNode($xpathExp, $dom) { $xpath = new DOMXPath($dom); $nodeList = $xpath->query($xpathExp); if ($nodeList->length != 1) { die("The given xpath expression is not valid"); } return $nodeList->item(0); } }//Creates XML string and XML document using the DOM $dom = new DomDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->load("test.xml"); $xmlAssistor = new XMLAssistor(); $xmlAssistor->showStruct($dom); // echo $xmlAssistor->selectSingleNode("//plot", $dom)->nodeName;?>
class XMLAssistor
{
private $struct_str = NULL; private function loop($node)
{
// iterate all attributes of the current node
$attrList = $node->attributes; foreach ($attrList as $childNode)
{
echo $childNode->nodeName . "[" . $childNode->nodeValue . "]<br>\n";
} // iterate all below elements
$nodeList = $node->childNodes; foreach ($nodeList as $childNode)
{
if ($childNode->nodeType == XML_ELEMENT_NODE)
{
echo $childNode->nodeName . "[" . $this->getElementTextValue($childNode) . "]<br>\n";
$this->loop($childNode);
}
}
} private function getElementTextValue($node)
{
$nodeList = $node->childNodes; foreach ($nodeList as $childNode)
{
if ($childNode->nodeType == XML_TEXT_NODE)
{
return $childNode->nodeValue;
}
} return "";
} private function checkForDom($dom)
{
if (!($dom instanceof DomDocument))
{
die("Parameter \"dom\" is not an instance of DomDocument tyep.");
}
} /**
* @desc show all elements of the given dom object
*/
public function showStruct($dom)
{
$root = $dom->documentElement;
$this->loop($root);
} /**
* @desc return a list of nodes which match the given xpath expression.
*/
public function selectNodes($xpathExp, $dom)
{
$xpath = new DOMXPath($dom);
$nodeList = $xpath->query($xpathExp); if ($nodeList->length == 1)
{
die("The given xpath expression is not valid");
} return $nodeList;
} /**
* @desc return a single node witch matches the given xpath expression.
*/
public function selectSingleNode($xpathExp, $dom)
{
$xpath = new DOMXPath($dom);
$nodeList = $xpath->query($xpathExp); if ($nodeList->length != 1)
{
die("The given xpath expression is not valid");
} return $nodeList->item(0);
}
}//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->load("test.xml");
$xmlAssistor = new XMLAssistor();
$xmlAssistor->showStruct($dom);
// echo $xmlAssistor->selectSingleNode("//plot", $dom)->nodeName;?>
KIKAN[2]
RANK[3]
LAT[34.582221]
LNG[136.111823]
KIKAN[1]
NO[2]
ROUTE[1号]
TYPE[1]
NO[1]
SUBNO[1]
CONTENT[車線規制]
RANK[3]
CAUSE[踏掛版補修工事]
CD[9]
SPLACE[水口町水口 ]
EPLACE[ ]
SDATE[2007/09/07 09:00]
CD[200709070930]
EDATE[2007/12/31 17:00]
CD[200712311700]
KISEI[]
KIKAN[3]
RANK[1]
LAT[34.525175]
LNG[135.455737]
KIKAN[京都国道]
NO[3]
ROUTE[24号]
TYPE[1]
NO[24]
SUBNO[1]
CONTENT[通行止]
RANK[1]
CAUSE[作業]
CD[11]
SPLACE[久御山町佐古 ]
EPLACE[安田交差点 ]
SDATE[2007/09/01 09:00]
CD[200709010900]
EDATE[]
CD[999999999999]
KISEI[]
KIKAN[5]
RANK[1]
LAT[34.3905]
LNG[134.584]
KIKAN[兵庫国道]
NO[5]
ROUTE[2号]
TYPE[1]
NO[2]
SUBNO[1]
CONTENT[通行止]
RANK[1]
CAUSE[災害]
CD[5]
SPLACE[相坂稲荷町 ]
EPLACE[杭瀬本町1丁目]
SDATE[2007/04/12 11:30]
CD[200704121130]
EDATE[2007/10/21 18:25]
CD[200710211825]
(
[LIST] => Array
(
[0] => Array
(
[_columnindex] => 0
[KISEI] => Array
(
[0] => Array
(
[_columnindex] => 0
[_attributes] => Array
(
[KIKAN] => 2
[RANK] => 3
[LAT] => 34.582221
[LNG] => 136.111823
) [KIKAN] => Array
(
[0] => Array
(
[_columnindex] => 0
[_attributes] => Array
(
[NO] => 2
) [_innertext] => 1
) ) [ROUTE] => Array
(
[0] => Array
(
[_columnindex] => 0
[_attributes] => Array
(
[TYPE] => 1
[NO] => 1
[SUBNO] => 1
) [_innertext] => 1号
) ) [CONTENT] => Array
(
[0] => Array
(
[_columnindex] => 0
[_attributes] => Array
(
[RANK] => 3
) [_innertext] => 車線規制
) )...........