尝试使用PHP将SimpleXMLObject转换为HTML
这是我第一次使用SimpleXMLobject,所以我有一个XMLstring,看起来像这样:尝试使用PHP将SimpleXMLObject转换为HTML,php,html,xml-parsing,Php,Html,Xml Parsing,这是我第一次使用SimpleXMLobject,所以我有一个XMLstring,看起来像这样: "<Body><SoapResponse><response><code>0</code><message>Success</message><id>123</id></response><details><a>123</a><b>AB
"<Body><SoapResponse><response><code>0</code><message>Success</message><id>123</id></response><details><a>123</a><b>ABC</c><d>1</d><e>XYZ</e><f>df</f><g>1</g><h>10</h></details><secondarydetails><box1><boxname>wer</boxname><date>2013-11-29</date><expirydate>2013-12-29</expirydate><flag>S</flag></box1><box2><boxname>pos</boxname><date>2013-11-29</date><expirydate>2013-12-29</expirydate><flag>E</flag></SoapResponse></Body>"
<pre><code>$xml = simplexml_load_string($soapresponse);
function writeList($items){
if($items === null)
return;
echo '<ul>';
foreach($items as $item => $children){
echo '<li>'.$item;
writeList($children);
echo '</li>';
}
echo '</ul>';
writeList($xml);</code></pre>
我试图将其解析并转换为html格式,最好是html表格,但我只找到以下代码:
但显示的html只是没有值的标记,如下所示:
- SoapResponse
- 响应
- 代码
- 消息
$xml = simplexml_load_string($soapresponse);
// You need to build the table wrapper first
echo '<table>';
// The recursive function
function writeList($items){
if($items === null)
return;
echo '<tr>';
foreach($items->children() as $i => $child){
echo '<td>', $child->getName(), '</td><td>', (string)$child, '</td>';
writeList($child);
}
echo '</tr>';
}
writeList($xml);
echo '</table>';
然后返回一个simpleXMLElement。如以下PHP文档所示:
这可能会让你更接近:
response
code 0
message Success
id 123
details
a 123
b ABC
d 1
e XYZ
f df
g 1
h 10
secondarydetails
box1
boxname wer
date 2013-11-29
expirydate 2013-12-29
flag S
box2
boxname pos
date 2013-11-29
expirydate 2013-12-29
flag E
$xml=simplexml\u load\u字符串($soapresponse);
//您需要首先构建表包装器
回声';
//递归函数
函数writeList($items){
如果($items==null)
返回;
回声';
foreach($items->children()作为$i=>$child){
回显“”,$child->getName(),“”,(字符串)$child“”;
作家(儿童);
}
回声';
}
writeList($xml);
回声';
它将生成如下表:
<?php
$news=new SimpleXMLElement('
<Body><SoapResponse><response><code>0</code><message>Success</message><id>123</id></response><details><a href="#">123</a><b>ABC</b><d>1</d><e>XYZ</e><f>df</f><g>1</g><h>10</h></details><secondarydetails><box1><boxname>wer</boxname><date>2013-11-29</date><expirydate>2013-12-29</expirydate><flag>S</flag></box1><box2><boxname>pos</boxname><date>2013-11-29</date><expirydate>2013-12-29</expirydate><flag>E</flag></box2></secondarydetails></SoapResponse></Body>');
print_r($news);
?>
回应
代码0
消息成功
身份证号码123
细节
a 123
b ABC
d 1
e XYZ
f df
g 1
H10
次要细节
框1
boxname wer
日期2013-11-29
截止日期2013-12-29
旗子
框2
boxname位置
日期2013-11-29
截止日期2013-12-29
旗E
在XML字符串中,缺少开始和结束标记,要将XML解析为HTML,可以使用SimpleXMLElement。它将xml转换为html,如下代码所示
有关更多信息,请参见非常感谢它的工作原理,但如果您不介意,我还有一个问题,如果我想将数组的标题(即响应、次要详细信息等)设置为不同的字体,我该如何做,我该如何更改simplexmlobject中的某些元素格式,我在谷歌上搜索如何通过其编号访问某些simplexmlobject并更改其格式。我没有找到多少帮助,你能帮我吗?
<?php
$news=new SimpleXMLElement('
<Body><SoapResponse><response><code>0</code><message>Success</message><id>123</id></response><details><a href="#">123</a><b>ABC</b><d>1</d><e>XYZ</e><f>df</f><g>1</g><h>10</h></details><secondarydetails><box1><boxname>wer</boxname><date>2013-11-29</date><expirydate>2013-12-29</expirydate><flag>S</flag></box1><box2><boxname>pos</boxname><date>2013-11-29</date><expirydate>2013-12-29</expirydate><flag>E</flag></box2></secondarydetails></SoapResponse></Body>');
print_r($news);
?>