Jump to content


Photo

PHP: Getting data out of an xml-file and write it to SQL-database


  • Please log in to reply
4 replies to this topic

#1 Highlord

Highlord
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 August 2006 - 10:01 AM

Ok,

I have a XML-file with the following structure:

<row>
<field name="id">12</field>
<field name="car">VW</field>
<field name="year">1992</field>
<field name="type">4x4</field>
<field name="fuel">Gas</field>
</row>

Offcourse with multipe <row>'s.
Now I have to write a PHP-script that gets the id, car, and type out of this structure and assigns it to a variable so i can put this in sql-database table (carid, carname, type).

I allready found a way to show the data I want on the screen, I just cant figure out how i get it in the database.

This is how I print the selected data on a screen:

  <?php
 
    $mblist  = '';
    $flag    = false;
$type = 0;
$hgid = array();
$normid= array();
$perc_code = array();



    function openElement($parser, $element, $attributes) {
        global $flag;
        if (($element == 'field') && ($attributes['name'] == 'id'))
{
$flag = true;
$type = 1;
}
if (($element == 'field') && ($attributes['name'] == 'car'))
{
$flag = true;
$type = 2;
}
if (($element == 'field') && ($attributes['name'] == 'type'))
{
$flag = true;
$type = 3;
}
    }

    function closeElement($parser, $element) {
        global $flag;
        $flag = false;
    }

    function characterData($parser, $data) {
        global $flag,$mblist;
        if ($flag)  $mblist[] = $data;
    }

    $parser = xml_parser_create();

    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($parser, "openElement", "closeElement");
    xml_set_character_data_handler($parser, "characterData");

    $document = file_get_contents("lassi.xml");
    xml_parse($parser, $document);

    xml_parser_free($parser);

    foreach ($mblist as $value) {
        echo $value.'<br/>';
    }

?>




I'd realy like some actual help with the scripting and not so much a link to a site or anything, because I've been trying to do this for 2days now and I think I've read every possible site about it, but just dont find how to do it  :(

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 August 2006 - 01:07 PM

When you run the foreach loop... what does it print out?

Info: PHP Manual


#3 Highlord

Highlord
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 August 2006 - 01:20 PM

In this example it prints out:
12
VW
4x4



#4 Highlord

Highlord
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 August 2006 - 02:26 PM

Nobody?  :(

#5 Highlord

Highlord
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 31 August 2006 - 08:54 AM

Going to bump this one last time, hope someone realy has a solution to this.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users