0

I have this format of xml which is created automatically and was wondering how to iterate through it and save data into mysql

<sales>

  <site_id="000001" sale_id="80" end_date="20170826" end_time="112420" />

  <site_id="000002" sale_id="81" end_date="20170828" end_time="101001" />

  <site_id="000003" sale_id="82" end_date="20170828" end_time="101001" />

</sales>

I am okay with inserting into database, however it is the iteration and capturing in a variable that I am kind of stuck with

4
  • this is not a valid XML, you need to parse like html but isn't valid HTML neither Commented Nov 16, 2017 at 10:58
  • It is valid - just use SimpleXML: php.net/manual/de/simplexml.examples-basic.php Commented Nov 16, 2017 at 11:01
  • 1
    it's NOT valid XML, the tag is closed as HTML, and the nodename is used as an attribute Commented Nov 16, 2017 at 11:08
  • Indeed, should be <site side_id="000" etc <site_id="000" is not valid XML so won't be parsed. Also need a closing tag </site> Commented Nov 16, 2017 at 11:11

2 Answers 2

1

If you can't edit the source you can use regex:

<?php
$xml = <<<XML
<sales>

  <site_id="000001" sale_id="80" end_date="20170826" end_time="112420" />

  <site_id="000002" sale_id="81" end_date="20170828" end_time="101001" />

  <site_id="000003" sale_id="82" end_date="20170828" end_time="101001" />

</sales>
XML;

$lines = preg_split("/\n/", $xml);

$results = array();
foreach($lines as $line)
{
    $line = trim($line);
    if(preg_match("/^<[^$]+\ \/>$/", $line))
    {
        preg_match_all("/([^=<\s]+)=\"([^\"]+)\"/", $line, $m);
        $result = array();
        foreach($m as $n)
        {
            $count = count($n);
            for($i=0;$i<$count;$i++)
                $result[$m[1][$i]] = $m[2][$i];
        }
        $results[] = $result;
    }
}

print_r($results);

PD: KILL THE XML SOURCE DEVELOPER

Sign up to request clarification or add additional context in comments.

2 Comments

One last question, what is the best way to capture the values into variables? [given that the xmls will be different in sizes and will constantly be changing, but the values must be stored in mysql ]
the code is prepared to have X attributes in the XML by each node, you have all the keys and values in the array, you only need to iterate it and insert the values in the correspondent key codes.
1

It's a not valid XML, it will be something like this:

<sales>

  <sale site_id="000001" sale_id="80" end_date="20170826" end_time="112420"></sale>

  <sale site_id="000002" sale_id="81" end_date="20170828" end_time="101001"></sale>

  <sale site_id="000003" sale_id="82" end_date="20170828" end_time="101001"></sale>

</sales>

$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadXML($xml);

$xpath = new DOMXpath($dom);
$a = $xpath->query("//sale");

$result = array();
$i = 0;
foreach($a as $b)
{
    if ($b->hasAttributes())
    {
        $j = 0;
        $tmp = array();
        foreach ($b->attributes as $attr)
        {
            $tmp[$attr->nodeName] = $attr->nodeValue;
            $j++;
        }
        $result[] = $tmp;
    }
    $i++;
}

print_r($result);

`

output:

Array
(
    [0] => Array
        (
            [site_id] => 000001
            [sale_id] => 80
            [end_date] => 20170826
            [end_time] => 112420
        )

    [1] => Array
        (
            [site_id] => 000002
            [sale_id] => 81
            [end_date] => 20170828
            [end_time] => 101001
        )

    [2] => Array
        (
            [site_id] => 000003
            [sale_id] => 82
            [end_date] => 20170828
            [end_time] => 101001
        )

)

5 Comments

Deleted my answer as this is much better.
I can't write multiline php var, sorry
This file comes remotely, so you are saying I may have to write a code that formats it into a correct xml before it can be parsed?
Yes, there is other alternative, that is read it as HTML instead of XML but, I can't read the "site_id"-s value because it looks like attribute but really is the name of node. Anyway I will try with regex.
One last question, what is the best way to capture the values into variables? given that the xmls will be different in sizes and will constantly be changing, but the values must be stored in mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.