php导入xml数据格式 phpxml( 三 )


!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
titleJSON/title
/head
body
script
function check() {
var XHR = new XMLHttpRequest();
XHR.open('GET','JSON.php');
XHR.onreadystatechange = function (){
if(XHR.readyState == 4XHR.status ==200){
var books =JSON .parse(XHR.responseText);
var books2='trthid/ththbookname/ththtime/ththprice/ththmarker/ththpublisher/th/tr'
for (var i=0;ibooks.length;i++){
books2+= 'trtd'+(books[i ].id)+'/tdtd'+(books[i ].name)+'/tdtd'+(books[i ].time)+'/tdtd'+(books[i ].jia)+'/tdtd'+(books[i ].zhe)+'/tdtd'+(books[i ].chu)+'/td/tr';
}
document.getElementById('table2').innerHTML=books2;
}
};
XHR.send(null);
}
/script
input type="button" value="https://www.04ip.com/post/点我" onclick="check();"
table id="table2" border="2" cellspacing="0"/table
/body
/html
PHP 读取和编写 XML什么是
XML?
XML
是一种数据存储格式 。它没有定义保存什么数据 , 也没有定义数据的格式 。XML
只是定义了标记和这些标记的属性 。格式良好的
XML
标记看起来像这样:
复制代码
代码如下:
nameJack
Herrington/name
DOM读取
XML
复制代码
代码如下:
?php
$doc
=
new
DOMDocument();
$doc-load(
'books.xml'
);
$books
=
$doc-getElementsByTagName(
"book"
);
foreach(
$books
as
$book
)
{
$authors
=
$book-getElementsByTagName(
"author"
);
$author
=
$authors-item(0)-nodeValue;
$publishers
=
$book-getElementsByTagName(
"publisher"
);
$publisher
=
$publishers-item(0)-nodeValue;
$titles
=
$book-getElementsByTagName(
"title"
);
$title
=
$titles-item(0)-nodeValue;
echo
"$title
-
$author
-
$publisher\n";
}
?

DOM
编写
XML
复制代码
代码如下:
?php
$books
=
array();
$books
[]
=
array(
'title'
=
'PHP
Hacks',
'author'
=
'Jack
Herrington',
);
$doc
=
new
DOMDocument();
//创建dom对象
$doc-formatOutput
=
true;
$r
=
$doc-createElement(
"books"
);//创建标签
$doc-appendChild(
$r
);
//将$r标签,加入到xml格式中 。
foreach(
$books
as
$book
)
{
$b
=
$doc-createElement(
"book"
);
//创建标签
$author
=
$doc-createElement(
"author"
);
$author-appendChild($doc-createTextNode(
$book['author']
));
//给标签添加内容
$b-appendChild(
$author
);
//将子标签
加入父标签
$r-appendChild(
$b
);
//加入父标签中!
}
echo
$doc-saveXML();
?

php导入xml数据格式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于phpxml、php导入xml数据格式的信息别忘了在本站进行查找喔 。

推荐阅读