1,105,456 Community Members

PHP Simple XML

Member Avatar
FakeTales
Light Poster
33 posts since Jan 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Self-Answered as of 1 Year Ago
Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Care to share what solved your issue?

Member Avatar
FakeTales
Light Poster
33 posts since Jan 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article