我在xml中的sparql输出如下:
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="judulNaskah"/>
<variable name="linkNaskah"/>
</head>
<results ordered="false" distinct="false">
<result>
<binding name="judulNaskah">
<literal>
Perancangan Aplikasi Fuzzy Multi Criteria Decision Making (FMCDM) Untuk Menentukan Nilai Ketidakpastian Sistem Pakar
</literal>
</binding>
<binding name="linkNaskah">
<literal>
http://seminar.ilkom.unsri.ac.id/index.php/ars/article/view/343
</literal>
</binding>
</result>
<result>
<binding name="judulNaskah">
<literal>
Implementasi Metode Fuzzy Sugeno Pada Robot Kapal Penghindar Rintangan
</literal>
</binding>
<binding name="linkNaskah">
<literal>
http://seminar.ilkom.unsri.ac.id/index.php/ars/article/view/80
</literal>
</binding>
</result>
</results>
</sparql>
我希望得到所有标签文字的价值,我试试这段代码:
$xml=simplexml_load_string($hasil) or die("Error: Cannot create object");foreach ($xml->results->result->binding as $node) {
$arr = $node->attributes(); // returns an array
print ($node->literal);
print ("<br>");
}
` 但我的输出只是给出结果数组[0],如下所示: Perancangan Aplikasi模糊多准则决策(FMCDM)Untuk Menentukan Nilai Ketidakpastian Sistem Pakar http://seminar.ilkom.unsri.ac.id/index.php/ars/article/view/343
我如何获得所有数组文字的值?
任何人都可以帮助我。
答案 0 :(得分:1)
好像你在数组中有对象数组。 请尝试以下代码
$xml=simplexml_load_string($hasil) or die("Error: Cannot create object");
foreach ($xml->results->result as $nodes) {
foreach ($nodes->binding as $node) {
echo $node->literal.'<br>';
}
}
//echo '<pre>'.print_r( $xml->results->result , true).'</pre>';
希望有所帮助
编辑部分
$xml = simplexml_load_string($hasil) or die("Error: Cannot create object");
$results = $xml->results->result;
for($i=0; $i<count($results); $i++) {
echo $i + 1;
echo ' .';
foreach ($results[$i]->binding as $key =>$node) {
$output=$node->literal;
$arr=$node->attributes();
if ($arr=="linkNaskah"){
echo "<a href=$output>$output</a><br><br>";
}
else {
echo $key.''.$output.'<br>';
}
}
}