![](/img/trans.png)
[英]PHP XML Feed - How to print value of specific child within parent. Parent has same name as other nodes
[英]PHP XML - How to build a tree of xml nodes that are at at same level with parent child relationship
我試圖從文件中讀取xml並像樹一樣顯示它們。 我正在使用PHP,我想得到如下所示的輸出。 xml文件的內容如下......
<Categories>
<Category>
<Id>1</Id>
<Name>Parent 1</Name>
<ParentId>1</ParentId>
<ParentName>Parent 1</ParentName>
</Category>
<Category>
<Id>2</Id>
<Name>Child 1</Name>
<ParentId>1</ParentId>
<ParentName>Parent 1</ParentName>
</Category>
<Category>
<Id>3</Id>
<Name>Child 2</Name>
<ParentId>1</ParentId>
<ParentName>Parent 1</ParentName>
</Category>
<Category>
<Id>8</Id>
<Name>Grand Child 1 -1</Name>
<ParentId>2</ParentId>
<ParentName>Child 1</ParentName>
</Category>
<Category>
<Id>12</Id>
<Name>Parent 2</Name>
<ParentId>12</ParentId>
<ParentName>Parent 2</ParentName>
</Category>
<Category>
<Id>15</Id>
<Name>Child 2-1</Name>
<ParentId>12</ParentId>
<ParentName>Parent 2</ParentName>
</Category>
</Categories>
</CategoryList>
我想閱讀這個xml文件(我知道如何閱讀它)但是我不能像下面那樣格式化它...我如何獲得最多父節點的所有節點並獲得這些父節點的子節點(使用遞歸或怎么樣)
<ul>
<li>Parent 1
<ul>
<li> Child 1
<ul>
<li>Grand Child 1 -1</li>
</ul>
</li>
<li> Child 2</li>
</ul>
</li>
<li>Parent 2
<ul>
<li>Child 2-1 </li>
</ul>
</li>
</ul>
請任何幫助將不勝感激....
編輯*到目前為止我做了什么......
$xml= simplexml_load_string('myxmlstring');
get_categories($xml, 0);
function get_categories($xml, $id) {
if ($id==0)
$Categories = $xml->xpath('Categories/Category[ParentId=Id]');
else
$Categories = $xml->xpath('Categories/Category[ParentId='.$id.' and Id!='.$id.']');
echo '<ul id="catlist'.$id.'">';
foreach($Categories as $Category) {
echo "<li>ID: " . $Category->Id . "--Name: " . $Category->Name;
get_categories($xml, $Category->Id);
echo "</li>";
}
echo "</ul>";
}
現在我只想確認這是最佳解決方案。 或者有人可以提出更好的想法......
ParentName過多,只需放置父ID即可。
由於您搜索每個節點,因此執行時間將為O(N 2 ),其中N是節點的數量。
但是,可以選擇在線性時間內執行此操作:首先遍歷數據並構建樹結構(或稍微),然后根據它遍歷該結構和輸出節點。
輸出緩沖在這里也是一個不錯的選擇。
// init
$childrenReferences = array();
$rootNodes = array();
$xmlNodes = array();
// gathering structure
$cats = $xml->getElementsByTagName('Category');
for ($i = 0; $i < $cats->length; $i++) {
$cat = $cats[$i];
$id = $children->Id;
$parentId = $cat->ParentId;
$xmlNodes[$id] = $cat;
if ($parentId == $id) {
$rootNodes []= $id;
continue;
}
if (array_key_exists($parentId, $childrenReferences)) {
$childrenReferences[$parentId] []= $id;
} else {
$childrenReferences[$parentId] = array($id);
}
}
// output
function out_nodes($nodes) {
global $childrenReferences, $xmlNodes; // this is not required since php 5.3 or something about
echo "<ul>";
foreach ($nodes as $id) {
$cat = $xmlNodes[$id];
echo "<li>ID: " . $cat->Id . "--Name: " . $cat->Name;
if (array_key_exists($id, $childrenReferences)) { // intermediate node
out_nodes($childrenReferences[$id]);
}
echo "</li>";
}
echo "</ul>";
}
ob_start();
out_nodes($rootNodes);
ob_end_flush();
代碼可能無法工作甚至編譯,但你明白了。
謝謝kirilloid ....這是很好的幫助...我正在使用代碼進行一些修改... xml來自curl我知道ParentName過多,但我無法控制它。 這是最終的代碼......
$childrenReferences = array();
$rootNodesIDs = array();
$xmlNodes = array();
$dom = new DOMDocument;
$dom->loadXML($xml);
$Categories = $dom->getElementsByTagName('Category');
$length = $Categories->length;
for ($i = 0; $i < $length; $i++) {
$cat = $Categories->item($i); //Get the DOMNode
$id = $cat->getElementsByTagName('Id')->item(0)->nodeValue;
$parentId = $cat->getElementsByTagName('ParentId')->item(0)->nodeValue;
$xmlNodes[$id] = $cat;
if ($parentId == $id) {
$rootNodesIDs []= $id;
continue;
}
if (array_key_exists($parentId, $childrenReferences)) {
$childrenReferences[$parentId] []= $id;
} else {
$childrenReferences[$parentId] = array($id);
}
}
function out_nodes($rootids) {
global $childrenReferences, $xmlNodes;
echo "<ul>";
foreach ($rootids as $id) {
$cat = $xmlNodes[$id];
echo "<li>ID: " . $cat->getElementsByTagName('Id')->item(0)->nodeValue . "--Name: " . $cat->getElementsByTagName('Name')->item(0)->nodeValue;
if (array_key_exists($id, $childrenReferences)) { // intermediate node
out_nodes($childrenReferences[$id]);
}
echo "</li>";
}
echo "</ul>";
}
ob_start();
out_nodes($rootNodesIDs);
ob_end_flush();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.