Php 将xml标记组合成一个

Php 将xml标记组合成一个,php,xml,Php,Xml,有没有一种简单的方法可以将xml文件标记转换为一行?我的xml文件($xml=simplexml\u load\u文件('http://localhost/locations.xml“);)包含: <markers> <marker> <name>Chipotle Minneapolis</name> <lat>44.947464</lat> <lng>-93.320826<

有没有一种简单的方法可以将xml文件标记转换为一行?我的xml文件(
$xml=simplexml\u load\u文件('http://localhost/locations.xml“);
)包含:

<markers>
    <marker>
    <name>Chipotle Minneapolis</name>
    <lat>44.947464</lat>
    <lng>-93.320826</lng>
    <category>Restaurant</category>
    <address>3040 Excelsior Blvd</address>
    <address2></address2>
    <city>Minneapolis</city>
    <state>MN</state>
    <postal>55416</postal>
    <country>US</country>
    <phone>612-922-6662</phone>
    <email>info@chipotle.com</email>
    <web>http://www.chipotle.com</web>
    <hours1>Mon-Sun 11am-10pm</hours1>
    <hours2></hours2>
    <hours3></hours3>
    <featured></featured>
  </marker>
</markers>

明尼阿波利斯奇波特酒店
44.947464
-93.320826
餐厅
怡东大道3040号
明尼阿波利斯
锰
55416
美国
612-922-6662
info@chipotle.com
http://www.chipotle.com
星期一至星期日上午11时至晚上10时
将上述代码转换为此格式:

<markers><marker name="Chipotle Minneapolis" lat="44.947464" lng="-93.320826" category="Restaurant" address="3040 Excelsior Blvd" address2="" city="Minneapolis" state="MN" postal="55416" country="US" phone="612-922-6662" email="info@chipotle.com" web="http://www.chipotle.com" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" featured="" features="" /></markers>


谢谢

将xml代码放入
$string
变量并使用以下代码:

$xml = new SimpleXMLElement('<markers/>');

$marker = $xml->addChild('marker');

$string = "<markers> ... </markers>";

$string = simplexml_load_string( $string );

foreach( $string->marker->children() as $name => $value )
    $marker->addAttribute( $value->getName(), $value );

echo $xml->asXML();
$xml=新的SimpleXMLElement(“”);
$marker=$xml->addChild('marker');
$string=“…”;
$string=simplexml\u load\u string($string);
foreach($string->marker->children()作为$name=>$value)
$marker->addAttribute($value->getName(),$value);
echo$xml->asXML();
轻松完成以下任务:

$string='…';
$xml=新的simplexmlement($string);
foreach($xml->标记为$m){
foreach($m->children()作为$c){
$m->addAttribute($c->getName(),$c);
$trash[]=$c;
}
foreach($t)未设置($t[0]);
}
echo$xml->asXML();
即使您有多个
标记
元素,它也会工作。当然,如果 如果只有一个,则不需要外部循环。请注意 元素在单独的迭代中被删除(通过
unset()
)以避免混乱
主要的一个。

。。。这甚至都不符合要求。。。它应该将
的子标记移动到属性中。这不仅不能解决问题,而且使用正则表达式处理xml不是一个好主意!关于
unset($c[0])
和其他删除元素的方法,请阅读以下内容:
$string = '<markers>...</markers>';

$xml = new SimpleXMLElement($string);

foreach ($xml->marker as $m) {
    foreach ($m->children() as $c) {
        $m->addAttribute($c->getName(), $c);
        $trash[] = $c;
    }
    foreach ($trash as $t) unset($t[0]);
}

echo $xml->asXML();