David Brooks
David Brooks

Reputation: 759

Create JSON from part of xml with php

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.

Upvotes: 0

Views: 44

Answers (2)

Sahil Gulati
Sahil Gulati

Reputation: 15141

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));

Upvotes: 1

Ghanshyam Dekavadiya
Ghanshyam Dekavadiya

Reputation: 153

{
"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..."
            ]
        }
    }]
}

}

Upvotes: 0

Related Questions