Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I put an xml parsed file into a database?

Status
Not open for further replies.

alohaaaron

Programmer
Mar 27, 2008
80
US
Hi, I don't have access to curl or open xml.

I'm trying to put data into a database. In the fragment of code below it prints out the xml file correctly. Here is the XML File.
$xml_data = "<?xml version=\"1.0\"?><member><Email>abc@home.com me</Email></member>";

How would I insert the email address into a database when I don't know what the variable is called that outputs the data? Even though the email address abc@home.com is printed out correctly as Email: abc@home.com I don't see how to capture that variable and put it into a database? It's not $element_name since that's the field name and it's not an attribute. Where is the output coming from?

//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
{
switch($element_name)
{
case "EMAIL":
echo "Email: "; break;
}
}
 
first, you should stop double posting.
second, upgrade your installation. noone sensible would maintain live servers on unsupported platforms.
third, curl has been bundled since 4.02
fourth you do not need simplexml. xml parsers have been bundled with php for a while now.

if you are not savvy enough to use the xml parsing tools that are bundled, you could also extract email addresses through pattern matching. preg_match() would do the trick. research on regular-expressions.info to learn about pattern matching syntax.

 
Thanks I think.

1. I don't have control over what my vendor has installed on their platform thus I am stuck with 4.x
2. They don't have the curl extension enabled
3. I am parsing the XML file, that wasn't my question.
4. Using regular expressions is an interesting idea but won't help in my situation.
5. I hope to hear from people that have helpful ideas and solutions.
 
Hi

alohaaaron said:
Here is the XML File.
If indeed that is the whole XML file, even [tt]strip_tags()[/tt] is enough.
alohaaaron said:
Using regular expressions is an interesting idea but won't help in my situation.
If you reject a suggestion then please explain why that would not help.


Feherke.
 
in case it helps other readers

Code:
<?php
echo '<pre>';
$xmlData = "<?xml version=\"1.0\"?><member><Email>abc@home.com me</Email></member>";
$p = xml_parser_create();
xml_parse_into_struct($p, $xmlData, $vals, $index);
xml_parser_free($p);
$element = 'email';
if(!isset($index[strtoupper($element)])):
	$email = null;
else:
	//assume only one value
	list($email['address'], $email['name']) = explode(' ', strtolower($vals[$index[strtoupper($element)][0]]['value']));
endif;
print_r($email);

echo '<hr/>';
//preg match method
preg_match_all('/<email>(.*?)<\/email>/i', $xmlData, $matches);
print_r($matches);
echo '<hr/>';
//strip tags method
//only useful for a static data format
$data = strip_tags($xmlData);
echo $data;
echo '<hr/>';
//simple xml method
$sxml = new SimpleXMLElement($xmlData);
print_r($sxml->Email);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top