我有一个像xmlns一样的xml -
<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>
我希望将它转换为json,如下所示,在php中使用xmlns -
"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"
}
}
]
}
}
我尝试过使用simple_load_string()它会发出类似命名空间警告的警告:xmlns:URI DailyXml不是绝对的。我想要xmlns属性。
如何解决这个问题?还有其他方法可以实现吗?
答案 0 :(得分:0)
目前,您只是将XML序列化为JSON。您需要读取数据并转换为您想要的JSON:
$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);
{
"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"
}
}
}
XML非常破碎。 xmlns属性定义元素的命名空间。命名空间是元素定义的格式。(如(X)HTML,Atom,...)
在XML中,xmlns的使用方式与&#34; id&#34;或&#34;名称&#34; ChannelInfo和RegionName元素的属性。标识文档中元素的东西。
在其他元素节点上,它只是无用的,重复的数据。信息已经在元素名称中。
更好的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>