Member Avatar for FakeTales

i have this xml file

<CATALOG>
    <CD>
        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
</CATALOG>

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

<?php

ini_set('display_errors',1); 
error_reporting(E_ALL);

//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>.

Member Avatar for 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.

commented: Thanks for sharing. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.