php解析xml报文的方法是DOMDocument:
解析方法如下:
$xmlstring = XML
xml version='1.0'?
document
cmd attr='default'login/cmd
loginimdonkey/login
/document
XML;
$dom = new DOMDocument();
$dom-loadXML($xmlstring);
print_r(getArray($dom-documentElement));
function getArray($node) {
$array = false;
if ($node-hasAttributes()) {
foreach ($node-attributes as $attr) {
$array[$attr-nodeName] = $attr-nodeValue;
}
if ($node-hasChildNodes()) {
if ($node-childNodes-length == 1) {
$array[$node-firstChild-nodeName] = getArray($node-firstChild);
} else {
foreach ($node-childNodes as $childNode) {
if ($childNode-nodeType != XML_TEXT_NODE) {
$array[$childNode-nodeName][] = getArray($childNode);
return $node-nodeValue;
return $array;
①定义和用法
simplexml_load_file() 函数把 XML 文档载入对象中.
如果失败,则返回 false.
②语法
simplexml_load_file(file,class,options,ns,is_prefix)参数 描述
file 必需.规定要使用的 XML 文档.
class 可选.规定新对象的 class.
options 可选.规定附加的 Libxml 参数.
ns 可选.
is_prefix 可选.
③实例
例子 1. Interpret an XML document
代码如下
php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
var_dump($xml);
exit('Failed to open test.xml.');
SimpleXMLElement Object
(
[title] = Example Title
...
)
可以使用php里的dom,如下:
$xmlStr=[文件位置];
$dom
=
new
DOMDocument('1.0');
-loadXML($xmlStr);
//根
$vogue=$dom-createElement('vogue');
$dom-appendChild($vogue);
//第一级子目录
$level0=$dom-createElement('level0');
//第一级子目录的属性
$attr=$dom-createAttribute('parentTypeID');
$attr-appendChild($dom-createTextNode('-1'));
........
//保存
$dom-saveXML($xmlStr);
但是我更推荐使用print来打印后输入文件.
因为dom的输出无法分行,样例如下:
$books
array();
[]
array(
'title'
'PHP
Hacks',
'author'
'Jack
Herrington',
'publisher'
"O'Reilly"
);
'Podcasting
books
foreach(
as
$book
{
book
title?php
echo(
$book['title']
/title
author?php
$book['author']
/author
publisher?php
$book['publisher']
/publisher
/book
/books
以上就是土嘎嘎小编为大家整理的关于phploadxml的信息相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!