Google Answers Logo
View Question
 
Q: PHP5 Code for phrasing XML Files ( No Answer,   3 Comments )
Question  
Subject: PHP5 Code for phrasing XML Files
Category: Computers > Programming
Asked by: shua-ga
List Price: $10.00
Posted: 22 Mar 2006 11:37 PST
Expires: 21 Apr 2006 12:37 PDT
Question ID: 710627
I need PHP5 Code that reads an array of XML file (Sample Range is
0100-1000.xml) and breaks each field down into a string so it can be
saved into a MySQL database. Bellow is a sample XML file.

~~~ This is an example of a XML File that needs to be read ~~~
<?xml version="1.0" encoding="iso-8859-1"?>
<testcell>
  <id>12345</id>
  <displayinfo>12345</displayinfo>
  <entered>2006-02-25 13:28:12</entered>
  <first>0</first>
  <image>./img/type1.jpg</image>
  <itemclass>1</itemclass>
  <itemid>12345</itemid>
  <name>Gear #1</name1>
  <version>37</version>
</testcell>

~~~~~ End of File ~~~~~
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP5 Code for phrasing XML Files
From: way2cool-ga on 24 Mar 2006 17:04 PST
 
Here's how:

<?

$xmlfiles = range(1,1);

foreach ( $xmlfiles as $i)
{
        $cols = "";
        $vals = "";

        $f = sprintf("%04d.xml", i);

        $testcell = simplexml_load_file($f);

        $a = get_object_vars($testcell);

        $cols = implode(",", array_keys($a));
        $vals = "'".implode("','", array_values($a))."'";

        $sql = sprintf("INSERT INTO testcell (%s) VALUES (%s)", $cols, $vals);

        print($sql."\n");

        // mysql_query($sql);
}

?>
Subject: Re: PHP5 Code for phrasing XML Files
From: shua-ga on 28 Mar 2006 12:17 PST
 
it is 99% there. I'm having a problem. The first file it is scanning
is 0000.xml. My first file is 0100.xml since there is no 0000 station.
I tried playing with the range but it always tries to start off at
0000.xml
Subject: Re: PHP5 Code for phrasing XML Files
From: shua-ga on 28 Mar 2006 13:20 PST
 
$f = sprintf("%04d.xml", i);


after a little bit of trial I found out that "i" should have been "$i"
and it was filling 0000 due to the formatting on the line. since there
was no i it just filled it with all zeros.

Thank You very much!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy