0

A vendor is going to be sending me some xml via an httppost to a script I am going to make. The script, when receives the httppost I need to somehow get the data from the xml and put it into my data-base.

I do have experience with all 3, PHP MYSQL and XML. I do know how to get values from XML using E4X but I do not know how to get values from XML using PHP (4.3.9).

so say the httppost is XML that looks like this:

<data>
<item sku="434322" price="15.00" color="blue" shape="round"/>
<item sku="434323" price="20.00" color="red" shape="square"/>
<item sku="434324" price="45.00" color="green" shape="triangle"/>
<item sku="434325" price="30.00" color="blue" shape="star"/>
</data>

How can I use PHP to loop through each node and get each attribute value?

Thanks!!

EDIT: It seems like getting the data from the XML is way complicated so if there is an easier way using something like CSV instead of XML please share that. The vendor did say they could send CSV or whatever format I want.

0

5 Answers 5

1

This is how you loop trough XML nodes with PHP 4.0

<?php
$dom=domxml_open_file('file.xml');
// Or from a string
$dom=domxml_open_mem($xmlString);

$root=$dom->document_element;

$items=$root->get_elements_by_tagname('item');

// Loop trough childNodes
foreach ($items as $item) {
    $sku=$item->get_attribute('sku');

            // Your work here
}

echo $dom->dump_mem();
// Or to a file
$dom->dump_file('filename.xml');
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use file_get_contents() to fetch the XML string from the POST request. But I am not quite sure that "php://input" already works in PHP4.

$xmlData = file_get_contents("php://input");

In PHP4 there is the extension domxml for working with XML. But I would strongly recommend using PHP5 and the DOM extension.

1 Comment

I believe: file_get_contents("php://input"); does work in 4. But I cannot use PHP5 right now.
0

To handle CSV data, I use str_getcsv()

while (($data = str_getcsv() !== FALSE)
{do something useful with $data}

For XML, I recommend the SimpleXML class as opposed to the XML Parser. Simple's not the most flexible, but it works just fine if all you're doing is reading in some data and saving it.

If the form data is just a text string, then you'd

$xml = simplexml_load_string($string)

And then

foreach ($xml->children() as $child)

{do something interesting}

Or you can just ditch the XML and dump it into an array with xml_parse_into_struct

$xmlparser = xml_parser_create();

xml_parse_into_struct($xmlparser,$xmldata,$values);

xml_parser_free($xmlparser);

1 Comment

SimpleXML requires PHP5, he needs a PHP4 solution
0

If your vendor can send it in any way, why not send it as:
Valid PHP code, just send it as an array. If the Vendors script are running as PHP themself then this would be even easier as they can output valid PHP code using: var_export().

Also you could try using the XMLparsing functions found at the comments at this page: http://nl2.php.net/manual/en/function.xml-parse.php they all look pretty okay.

Comments

0

CSV would probably be simpler. You can use explode() to split the string (doc at https://www.php.net/explode).

For example, you can split the input into lines, then split each line into pieces on the commas:

// split up lines
$lines = explode("\n", $_POST['data']);
$data = array();
foreach ($lines as $row) {
    // split a line on the commas
    $prop = explode(',', $row);

    // append an element to the array that contains 
    // the properties as an associative array
    $data[] = array('sku' => $prop[0],
                    'price' => $prop[1],
                    'color' => $prop[2],
                    'shape' => $prop[3]);

}

Here, I'm assuming you want to convert the input into an array of elements, each element of the form:

array(4) {
  ["sku"] =>
  "434322",
  ["price"] =>
  "15.00",
  ["color"] => 
  "blue"
  ["shape"] =>
  "round"
}

Comments

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.