0

I am currently trying to parse an xml document with php. All is going well apart from one thing. Here is a sample of my xml:

<properties>
  <property>
    <propertyid>1</propertyid>
    <flags>
      <flag>This is a flag</flag>
      <flag>This is another flag</flag>
      <flag>This is yet another flag</flag>
      <flag>etc...</flag>
    </flags>
  </property>
  <property>
    ...
  </property>
<properties>

As you can see there are multiple <property> nodes. I am using SimpleXML which works perfectly fine. I just loop through each <property> with php and get the values, for example:

foreach ( $xml->$property as $property )
{
  echo $property->propertyid;
}

The problem is the <flag> nodes. Basically, I want to end up with JSON that looks like this:

{
  "flags1":
  {
    "flag": "This is a flag"
  },
  "flags2":
  {
    "flag": "This is another flag"
  },
  "flags3":
  {
    "flag": "This is yet another flag"
  }
  ,
  "flags4":
  {
    "flag": "..."
  }
}

There will be an indeterminate amount of flags for each property. I have tried a foreach loop to get the flag values which worked but then how can I get the values looking like the example JSON?

My foreach loop looks like this:

$flags = $property->flags;
foreach ( $flags->flag as $index => $flag )
{
  $arr = array($index => (string)$flag);
  echo json_encode($arr);
}

Which returns:

{"flag":"This is a flag"}{"flag":"This is another flag"}{"flag":"This is yet another flag"}{"flag":"etc..."}

This is so nearly there, I just don't know how to get it to be correct.

2 Answers 2

1

Hope this one will be helpful. Here we are using simplexml_load_string

Try this code snippet here

$result=array();
$counter=0;
$xml=simplexml_load_string($string);
foreach($xml->property as $data)//iterating over properties
{
    foreach($data->flags->flag as $flag)//iterating over flag
    {
        $result["flag$counter"]=array("flag"=>(string)$flag);
        $counter++;
    }
}

print_r(json_encode($result,JSON_PRETTY_PRINT));
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this is perfect :)
@DesignSubway Glad to help you friend... :)
0
{
"properties": {
    "parsererror": {
        "-style": "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black",
        "h3": [
            "This page contains the following errors:",
            "Below is a rendering of the page up to the first error."
        ],
        "div": {
            "-style": "font-family:monospace;font-size:12px",
            "#text": "error on line 14 at column 13: Extra content at the end of the document"
        }
    },
    "property": [{
        "propertyid": "1",
        "flags": {
            "flag": [
                "This is a flag",
                "This is another flag",
                "This is yet another flag",
                "etc..."
            ]
        }
    }]
}

}

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.