i have this xml file

        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>

with my php code i am able to get the parent node <CATALOG> then all the other data such as <TITLE> etc etc , how would i go about getting the <CD> tag?? Here is the php code



//recieves filename from client
$xml_filename = './xml/'.$_REQUEST['fileName'];

//loads xml file
$xmlobjects = simplexml_load_file($xml_filename,"SimpleXMLElement", LIBXML_NOERROR);

//check for valid xml file, '===' compares type
if ($xmlobjects === FALSE)
    $jsonString = '{ "error" : "The XML file that has been selected is invalid, please select another." }';
    echo $jsonString;
} else {

//first xml tag
$rootnode = $xmlobjects->getName();

//Parent Node Name
$parentNode = $rootnode;

//number of nodes within xml document
$childrenNodes = $xmlobjects->children();

//attributes within node
$firstchild = $childrenNodes[0];

$jsonString = '{ "parentNode" : [';

$jsonString = $jsonString.'"'.$parentNode.'" ,';

$jsonString = substr_replace($jsonString ,"",-1);

//names of the attributes within the child node (table headings)
$jsonString = $jsonString.'], "childNodeHeadings" : [';

foreach ( $firstchild as $element )
  $jsonString = $jsonString.'"'.$element->getName().'" ,';

//remove extra ","
$jsonString = substr_replace($jsonString ,"",-1);

//start new variable
$jsonString = $jsonString.'], "childData" : [';

//all elements values into a JSON array
foreach ( $childrenNodes as $child )
  $jsonString = $jsonString.'[';
  foreach ( $child as $element => $value ) 
    $jsonString = $jsonString.'"'.$value.'",';
  $jsonString = substr_replace($jsonString ,"",-1);
  $jsonString = $jsonString.'],';
//remove extra ","
$jsonString = substr_replace($jsonString ,"",-1);

//end array and JSON string
$jsonString = $jsonString.']}';

//output JSON stirng
echo $jsonString;  



bare in mind i am opening multiple xml files so each node has different titles , so i could be opening up a film xml doc so <CD> would now be <FILM>.

Edited by FakeTales

4 Years
Discussion Span
Last Post by FakeTales

Hey pritaeas sorry i was meant to post the sorce code after i solved it

so here is what i done

First of all i managed to pull the <CD> value out however it pulled out 5 of them , as i had 5 <CD> child nodes within the xml file. This was the code that pulled out all the child nodes

$jsonString = $jsonString.'], "childNodeHeading" : [';

foreach ($childNodeHeading as $element )
  $jsonString = $jsonString.'"'.$element->getName().'" ,';


so this was the output from the php file when queried in the browser

{ "parentNode" : ["CATALOG" ], 
"childNodeHeading" : ["CD" ,"CD" ,"CD" ,"CD" ,"CD" ], 
"subChildNodeHeadings" : ["TITLE" ,"ARTIST" ,"COUNTRY" ,"COMPANY" ,"PRICE" ,"YEAR" ], 
"subChildData" : [["Empire Burlesque","Bob Dylan","USA","Columbia","10.90","1985"],["Hide your heart","Bonnie Tyler","UK","CBS Records","9.90","1988"],
["Greatest Hits","Dolly Parton","USA","RCA","9.90","1982"],["Still got the blues","Gary Moore","UK","Virgin records","10.20","1990"],
["Eros","Eros Ramazzotti","EU","BMG","9.90","1997"]]}

i only needed one <CD> catalog to show as i am pasing this back later to an addXml.php file and it is used to create a new child node for the new data going to be passed in , so i modified the code above to this

$jsonString = $jsonString.'], "childNodeHeading" : [';

$i =1;
foreach ($childNodeHeading as $element )
  $jsonString = $jsonString.'"'.$element->getName().'" ,';
  if($i==1) break;

$jsonString = substr_replace($jsonString ,"",-1);

so as soon as it found one child node it would break the foreach loop , and in return give me back just one <CD>

i dont know if this is best practice but it does the job and i am able to add new data aswell.

Thanks for sharing.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.