0

I have a xml like below with xmlns -

  <ChannelInfo xmlns="Vijayabank_43_vijayBank1">
  <RegionName xmlns="Vijayabank_43_vijayBank1_Prime">
    <Width xmlns="Width">768</Width>
    <Height xmlns="Height">614</Height>
    <Top xmlns="Top">100</Top>
    <Left xmlns="Left">20</Left>
    <Audio xmlns="Audio">1</Audio>
  </RegionName>
  <RegionName xmlns="Vijayabank_43_vijayBank1_SubPrime">
    <Width xmlns="Width">452</Width>
    <Height xmlns="Height">614</Height>
    <Top xmlns="Top">100</Top>
    <Left xmlns="Left">808</Left>
    <Audio xmlns="Audio">0</Audio>
  </RegionName>
 <RegionName xmlns="Vijayabank_43_vijayBank1_Banner1">
   <Width xmlns="Width">1240</Width>
   <Height xmlns="Height">54</Height>
   <Top xmlns="Top">714</Top>
   <Left xmlns="Left">20</Left>
   <Audio xmlns="Audio">0</Audio>
 </RegionName>
</ChannelInfo>

I want to get it converted into json like below with xmlns in php -

  "ChannelInfo": {
   "-xmlns": "Vijayabank_43_vijayBank1",
   "RegionName": [
     {
       "-xmlns": "Vijayabank_43_vijayBank1_Prime",
       "Width": {
       "-xmlns": "Width",
       "#text": "768"
     },
       "Height": {
        "-xmlns": "Height",
        "#text": "614"
     },
     "Top": {
       "-xmlns": "Top",
       "#text": "100"
     },
     "Left": {
       "-xmlns": "Left",
       "#text": "20"
     },
     "Audio": {
       "-xmlns": "Audio",
       "#text": "1"
     }
   },
   {
     "-xmlns": "Vijayabank_43_vijayBank1_SubPrime",
     "Width": {
       "-xmlns": "Width",
       "#text": "452"
     },
     "Height": {
      "-xmlns": "Height",
      "#text": "614"
    },
     "Top": {
       "-xmlns": "Top",
       "#text": "100"
     },
     "Left": {
       "-xmlns": "Left",
      "#text": "808"
    },
    "Audio": {
      "-xmlns": "Audio",
       "#text": "0"
     }
   },
   {
    "-xmlns": "Vijayabank_43_vijayBank1_Banner1",
    "Width": {
      "-xmlns": "Width",
      "#text": "1240"
    },
    "Height": {
      "-xmlns": "Height",
      "#text": "54"
    },
    "Top": {
      "-xmlns": "Top",
      "#text": "714"
    },
    "Left": {
      "-xmlns": "Left",
      "#text": "20"
    },
    "Audio": {
      "-xmlns": "Audio",
      "#text": "0"
     }
   }
   ]
   }
   } 

I have tried with simple_load_string() it throws warning like namespace warning :xmlns: URI DailyXml is not absolute . I want xmlns attribute.

How to solve this?Is there any other way to achieve it.?

2
  • This is some seriously wrong XML. "xmlns" is a namespace for an element. That is the format definition the element or attribute belongs to. Commented May 7, 2014 at 13:48
  • @ThW thank but i cannot change the xml .is there is any otherway to achieve it? Commented May 20, 2014 at 12:16

1 Answer 1

1

At the moment you're just serializing the XML into JSON. You need to read the data and convert into into the JSON you would like:

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

$channel = $dom->documentElement;
$result = [
  'id' => $channel->namespaceURI,
  'Regions' => []
];
foreach ($xpath->evaluate('*', $channel) as $region) {
  $regionId = $region->namespaceURI;
  $result['Regions'][$regionId] = [];
  foreach ($xpath->evaluate('*', $region) as $property) {
    $result['Regions'][$regionId][$property->namespaceURI] = 
      trim($property->textContent);
  }
}

echo json_encode($result, JSON_PRETTY_PRINT);

Output: https://eval.in/154181

{
    "id": "Vijayabank_43_vijayBank1",
    "Regions": {
        "Vijayabank_43_vijayBank1_Prime": {
            "Width": "768",
            "Height": "614",
            "Top": "100",
            "Left": "20",
            "Audio": "1"
        },
        "Vijayabank_43_vijayBank1_SubPrime": {
            "Width": "452",
            "Height": "614",
            "Top": "100",
            "Left": "808",
            "Audio": "0"
        },
        "Vijayabank_43_vijayBank1_Banner1": {
            "Width": "1240",
            "Height": "54",
            "Top": "714",
            "Left": "20",
            "Audio": "0"
        }
    }
}

The XML is pretty broken. The xmlns attribute defines the namespace for an element. A namespace is the format the element is defined in. (Like (X)HTML, Atom, ...)

In your XML the xmlns is used like an "id" or "name" attribute on the ChannelInfo and RegionName elements. Something that identifies the element in the document.

On the other element nodes it is just useless, duplicate data. The information is already in the element name.

Better XML:

<Channel xmlns="http://domain.tld/ns/ads" id="Vijayabank_43_vijayBank1">
  <Region id="Vijayabank_43_vijayBank1_Prime">
    <Width>768</Width>
    <Height>614</Height>
    <Top>100</Top>
    <Left>20</Left>
    <Audio>1</Audio>
  </Region>
  ...
</Channel>
Sign up to request clarification or add additional context in comments.

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.