Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I was wondering if anyone knows a script or function that can format any xml file to a array in a specific format such as having a xml something like this (but much longer)

<data>
    <width>3.5</width>
    <height>2</height>
    <edge>.125</edge>
    <safe>.125</safe>
</data>

<fold>
    <show_lines></show_lines>
    <type>online</type>
</fold>

<preview>
    <name>testfile</name>
    <location>testurl.com</location>
</preview>

<preview>
    <name>myfile</name>
    <location>someurl.com</location>
    <special>frontandback</special>
</preview>

Id basically want to loop through each attribute check if there is more inside of it and so on and so on and basically create a array so it would look like so

$array = [data] (
    width=>3.5
    height=>2
    edge=>.125
    safe=>.125
)

[fold] (
    type=>online
)

[preview] (
    [0]=>(
        name=>testfile
        location=>testurl.com
    )
    [1]=>(
         name=>myfile
         location=>someurl.com
         special=>frontandback
    )
)

and so on so basically it will only grab the elements that have values and skip the ones that dont and some parts of the xml may have more children than the other part and it should grab them all and if there is multiple attributes with same name that would be a array of its own with each one being a array inside of it

Hope that makes sense can anyone help please. Im mainly trying to use simplexml but can use anythign else

share|improve this question

closed as not a real question by hakre, NullPoiиteя, Ocramius, tereško, Second Rikudo Apr 1 '13 at 8:27

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This can help you: outlandishideas.co.uk/blog/2012/08/xml-to-json –  SoldierCorp Mar 31 '13 at 8:38
    
Yes it is possible. It needs to be programmed. It has been programmed before (use the search). What have you tried? Where do you hit the roadblock? –  hakre Apr 1 '13 at 8:05

1 Answer 1

For simple documents like the one in your example there's always this (dirty) trick:

$arr = json_decode(json_encode(simplexml_load_string($xml)), 1);

working example

output:

Array (
    [data] => Array (
        [width]  => 3.5
        [height] => 2
        [edge]   => .125
        [safe]   => .125
    )
    [fold] => Array (
        [show_lines] => Array ()
        [type]       => online
    )
    [preview] => Array (
        [0] => Array (
                [name]     => testfile
                [location] => testurl.com
        )
        [1] => Array (
                [name]     => myfile
                [location] => someurl.com
                [special]  => frontandback
        )
    )
)
share|improve this answer
    
might also add - empty strings and arrays will both satisfy empty() - if you really needed to remove all nodes without values you could run through and unset() them afterwards. –  Emissary Mar 31 '13 at 14:06
    
The only problem with this method is that I get two preview objects instead of one with a array of both –  Yeak Mar 31 '13 at 21:23
    
@Yevo are you sure... My output pretty much matches the desired output in your question with the exception to fold=>show_lines been present? I can only see one preview array which is a 2D array? –  Emissary Mar 31 '13 at 21:26
    
You are actually correct it is coming out fine. thank you –  Yeak Apr 1 '13 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.