3

I have some simple XML which I would like to parse into an array in PHP so that I can build some dynamic SQL inserts.

Sample XML:

<Data>
    <Key>1</Key>
    <Column>Value 1</Column>
    <Column2>Value 2</Column>
</Data>

Data will be passed to PHP via http POST.

What's the easiest way to do this? There should always be one instance of but would like the option to take multiple instances from one POST.

I can certainly build out some code to parse specific PHP, but I'd like to use a single piece of code to handle the data dynamically so that it can be used to create inserts for multiple tables.

2 Answers 2

4

Well, assuming that you want a result of

array(
    1 => array(
        'Value 1',
        'Value 2',
    )
)

And assuming a structure of

<root>
    <Data>
        <!-- key and column entities -->
    </Data>
    <Data>
        <!-- key and column entities -->
    </Data>
    <Data>
        <!-- key and column entities -->
    </Data>
</root>

You could do something like (I prefer DomDocument):

$dom = new DomDocument();
$dom->loadXml($xmlString);
$dataElements = $dom->getElementsByTagName('Data');
$array = array();
foreach ($dataElements as $element) {
    $subarray = array();
    foreach ($element->childNodes as $node) {
        if (!$node instanceof DomElement) {
            continue;
        }
        $key = $node->tagName;
        $value = $node->textContent;
        $subarray[$key] = $value;
    }
    $array[] = $subarray;
}

With that edit, it'll turn:

<root>
    <Data>
        <Key>4</Key>
        <Foo>Bar</Foo>
    </Data>
    <Data>
        <Key>6</Key>
        <Bar>Baz</Bar>
    </Data>
</root>

Into:

array(
    array(
        'Key' => 4,
        'Foo' => 'Bar',
    ),
    array(
        'Key' => 6,
        'Bar' => 'Baz',
    ),
)
Sign up to request clarification or add additional context in comments.

2 Comments

My goal is to not have to hard-code Key, Column and Column2 - the format could be <root><data> ... </data><data> ... </data></root> but what's inside data will be variable. i guess i'm just looking for a function that will return all element names inside of <data> so i can then extract the value for that element. I've tried simplxml with $xmlstr = "<Data><Key>1</Key><Column>AAA</Column></Data>"; $sxe = new SimpleXMLElement($xmlstr); echo $sxe->getName() . "\n"; foreach ($sxe->children() as $child) { echo $child->getName() . "\n"; } but it returns: Data Key Column
Isn't that what you want it to return? I'm confused as to what you're trying to accomplish... If you just want by the name of the tag, I'll edit my answer with that...
1

SimpleXML is the name of the library what you are searching for: http://php.net/manual/en/book.simplexml.php

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.