Would someone draw up a Q&D example of parsing XML into
Moderator: General Moderators
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Would someone draw up a Q&D example of parsing XML into
Just a quick script to get me started on parsing XML data into a PHP variable.
Regards,
Regards,
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Have you read this? http://uk.php.net/simplexml
Code: Select all
$xml = new SimpleXMLElement(file_get_contents('/path/to/xml/file.xml'));
echo $xml->someTag;-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
OK. Now I am able to put certain bits of the XML data into variables using:
My plan is to insert all data from an XML file into a MySQL DB. How would it be possible to scan the $xml array and insert what data is found into a MySQL DB. I know the part of inserting into a DB, I'm just a bit lost on how you would extract the data from an XML document to be able to insert it.
Code: Select all
$newVar = $xml->stephen->age;- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I see it in the ManualOren wrote:And what is SimpleXMLElement d11wtq? I don't see it in the manual
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I have this code, which works, to a certain degree.
But this creates an infinite loops of the first name in my XML file. If I put
after the echo it only prints the first tags data.
Code: Select all
$xml = simplexml_load_file('data.xml');
while (list($k) = each ($xml)) {
echo $k;
}Code: Select all
break;- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I have already tried with
But I can only print the data held within the <stephen> tags.
I'm unsure of how to get it to scan through the XML and print out
"ALL" being everything.
Code: Select all
foreach ($xml->stephen->age as $value) {
echo $value;I'm unsure of how to get it to scan through the XML and print out
Code: Select all
$xml->ALL->ALL-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
This is sorted now. The error was in my XML, not my PHP.
Thank you
Code: Select all
$xml = simplexml_load_file('data.xml');
foreach ($xml->friend as $friend) {
printf("Name: %s <br> ", $friend->name);
printf("Age: %s <br> ", $friend->age);
}Oh yeah, I see it too now. I simply searched in the Function List (http://us2.php.net/manual-lookup.php?pa ... XMLElement) and couldn't find itd11wtq wrote:I see it in the Manualhttp://uk.php.net/simplexml I've never used it before, but it's on that page when you read the code examples.
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Have you ever parsed XML using before?
Code: Select all
xml_set_element_handler-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Could you give me a little help on how you run through an XML file and extract each tag into a MySQL DB. If I have 3 pieces of information inside a tag. For example
And I have a MySQL DB with the colums: ID, Name, Age, Sex. With these I want to insert the name, age & sex of each person onto a row in the DB.
For doing so I have to use because of its extended functionality compared to
I have thought of a couple of ways to do so. I'm currently in the process of doing the following:
For the 3 arguments for xml_set_character_data_handler (start tag, tag contents & end tag) I will leave the start and end tag functions empty and then in the tag contents function I will have the following code:
I know this doesn't work and it's just to show you an idea I had. I know you'll understand me easier via code than English.
Hope you can help, Stephen.
Code: Select all
<friends>
<friend>
<name> Stephen </name>
<age> 21 </name>
<sex> Male </name>
</friend>
<friend>
<name> Scott </name>
<age> 20 </age>
<sex> Male </age>
</friend>
<friend>
<name> Helena </name>
<age> 20 </age>
<sex> Female </sex>
</friend>
</friends>For doing so I have to use
Code: Select all
xml_set_character_data_handlerCode: Select all
simplexml_load_fileFor the 3 arguments for xml_set_character_data_handler (start tag, tag contents & end tag) I will leave the start and end tag functions empty and then in the tag contents function I will have the following code:
Code: Select all
function tag_contents($parser, $data) {
if ($j =<4) {
switch ($i) {
case 0:
mysql_query("INSERT INTO test (name) VALUES ('$data')");
$i++;
$j++;
break;
case 1:
mysql_query("INSERT INTO test (age) VALUES ('$data')");
$i++;
$j++
break;
case 2:
mysql_query("INSERT INTO test (sex) VALUES ('$data')");
$i++;
$j++;
break;
}
else $j = 0;
}
}I know this doesn't work and it's just to show you an idea I had. I know you'll understand me easier via code than English.
Hope you can help, Stephen.