extracting data from an xml document with php



So for about a week now I've been working on connecting to a MySQL database via PHP (done), extracting the data from an XML file and then storing that extracted data into an existing table within the database.


One of the fields I'm extracting works fine and is done like this:



foreach ($c02 as $c03) {
// finds 1st children of c02 node (under c01/c02[0], poetry by author) stored as variable $first_gen
foreach ($c03->children() as $first_gen) {
// bio of author stored under xml tag <bioghist>
if($first_gen->getName()=='bioghist') {
// sometimes comprised of several paragraphs so iterate over <p> tags under <bioghist> to get all data
foreach ($first_gen->p as $key=>$value) {
$bio .= $value;
}
}
}
}


This grabs the data from within each <p> tag that is a child of a <bioghist> tag. Some other data I am trying to extract however is stored within the attributes of a tag so I am trying to use XPath syntax to get this data:



foreach ($c02 as $c03) {
// finds 1st children of c02 node (under c01/c02[0], poetry by author) stored as variable $first_gen
foreach ($c03->children() as $first_gen) {
if($first_gen->getName()=='controlaccess') {
// traverse children tags under 'controlaccess'
foreach ($first_gen->children() as $second_gen) {
// locate 'persname' tag which contains variety of author data
if ($second_gen->getName()=='persname') {
// attributes: "forename" for $firstName, "a" for $lastName & "y" for $dob (as strings), under <emph> xml tag
$firstName .= $second_gen->xpath('//c02[0]//emph[@altrender="forename"]');
$lastName .= $second_gen->xpath('//c02[0]//emph[@altrender="a"]');
$dob .= $second_gen->xpath('//c02[0]//emph[@altrender="y"]');
}
}
}
}
}


This second set of data all lies under the same <emph> tag but with altrender attributes of forename, a and y. It won't extract this data for me; can anyone see what I am doing wrong?


I am then storing all these saved variables into an array which I am hoping to be able to pass into a MySQL table.


Thanks for your help!


No comments:

Post a Comment