PHP: Using simplexml to loop through all levels of an XML file -


I have a function that uses simplexml to return to the first level of nodes in the XML file and make it one Writes in the unordered list:

  function printAssetMap () {$ xml = simplexml_load_file (X_ASSETS); $ Property = $ xml- & gt; Property; $ Html = '& lt; Ul & gt; "\ N"; Foreign Currency ($ assets as $ assets) {$ html. = '& Lt; Li id = "asset '. $ Asset- & gt; asset_assetid." "& Gt;  In & gt; & lt; / ins> gt; ['$ Asset-> Asset_assetid.']  

XML I am using, which has nested nodes:

  & lt ;? Xml version = "1.0"? & Gt; & lt; asset & gt; & gt; asset & gt; & lt; ASSET_NAME & gt; Home & lt; / ASSET_NAME & gt; & lt; Asset_url & gt; / home & lt; / asset_url & gt; & lt; asset_assetid & gt; 1 & lt; / asset_assetid & gt; & lt; / asset & gt; & lt; asset & gt; & lt; ASSET_NAME & Gt; Projects & lt; / ASSET_NAME & gt; & lt; asset_url & gt; / Projects & lt; / asset_url & gt; & lt; asset_assetid & gt; 2 & lt; / asset_assetid & gt; & gt; Property & gt; ; & Lt; ASSET_NAME & gt; Portfolio & lt; / ASSET_NAME & lt; asset_url & gt; / Projects / Portfolio & lt; / asset_url & gt; asset_assetid & gt; 3 & lt; / asset_assetid & gt; & lt; / property & Gt; & lt; asset & gt; & lt; asset_name & gt; current jobs & lt; / Asset_name & gt; & Lt; Asset_url & gt; / Projects / current jobs & lt; / Asset_url & gt; & Lt; Asset_assetid & gt; 4 & lt; / Asset_assetid & gt; & Lt; / Property & gt; & Lt; / Property & gt; & Lt; / Property & gt;  

Now, I am starting to add the nodes of the nodes, which I am currently returning. Is there a way of loop through all the levels of hair nodes in an xml file, even if I do not know how many levels are there, and adding them to my list?

& lt; Asset / & gt; Takes the child of the current node, creates HTML, checks that the and are in their current nodes and remain under the tree deep.

Here's how you can do this:

  function printAssetMap () {return printAssets (simplexml_load_file (X_ASSETS)); } PrintAssets (SimpleXMlelement $ parent) {$ html = "& lt; ul & gt; \ n"; Foreign Currency ($ parent-> assets as asset $ asset) {$ html.} = Print asset ($ asset); } $ Html = "& Lt; / ul & gt; \ n"; Return $ Html; } Function printAsset (SimpleXMlelement $ asset) {$ html = '& lt; Li id = "property '. $ Asset- & gt; asset_assetid." "& Gt; & lt; in & gt; & amp; nbsp; & lt; / ins & gt; & lt; a href =" # "& gt; '$ asset- & gt; ASSET_NAME.' ['$ Asset-> Asset_assetid.'] & Lt; / a & gt; '; if (isset ($ asset- & gt; property)) is  & gt; children $ Html = printasset ($ asset);} $ html. = "& Lt; / Li & gt; \ N "; Return $ Html;}  

By the way, I really expect print to be a function called" printX "or instead of returning it Echo something, maybe you should name those functions as "buildex"?


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -