2014-11-04 55 views
0

我已阅读stackoverflow这里的其中一个问题,其中一位用户与@Mifas和@Abhishek Madhani分享此link,这是行得通的。如何格式化php数组?

现在,我想格式化XML输出是这样的:

<Form xmlns="url" label="Home Visit Form" type="TextView" name="FormName"> 
    <Field name="Category" label="FormType" value="Form1" type="TextView"/> 
    <Field type="Delimiter"/> 
    <Field name="ContractNumber" label="Contract number" value="3300000011" type="TextView"/> 
    <Field type="Delimiter"/> 
    <Field name="ClientName" label="Name of Client" value="Neplatic Neplaticovic" type="TextView"/> 
    <Field name="BirthDate" label="Birthday" value="1980-07-04" type="TextView"/> 
    <Field name="DueDate" label="Due Date" value="2014-07-24" type="TextView"/> 
</Form> 

但我不知道怎么办。

PHP:

header('Content-Type: text/xml'); 

$Form= array(
    0 => array(
     'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 
     'xsi:noNamespaceSchemaLocation' => 'http://homecredit.net/muchserver/ws/visit/types', 
     'label'=> 'Home Visit Form', 
     'type' => 'TextView', 
     'name' => 'HomeVisitForm' 
    ), 
    1 => array(
     'id' => '002', 
     'name' => 'Ijas', 
     'subjects' => array('Science','History','Social') 
    ) 
); 

// creating object of SimpleXMLElement 
$xml_Form = new SimpleXMLElement("<?xml version=\"1.0\"?><Form></Form>"); 
array_to_xml($Form,$xml_Form);     // function call to convert array to xml 
print $xml_Form->asXML();        //saving generated xml file 


// function defination to convert array to xml 
function array_to_xml($Form, &$xml_Form) { 
    foreach($Form as $key => $value) { 
     $key = is_numeric($key) ? "Field$key" : $key; 
      (is_array($value))    
       ? array_to_xml($value, $xml_Form->addChild("$key")) 
       : $xml_Form->addChild("$key","$value"); 
    } 
} 

?> 

输出上面的PHP代码:

<Form> 
    <Field0> 
     <xsi>http://www.w3.org/2001/XMLSchema-instance</xsi> 
     <noNamespaceSchemaLocation>url</noNamespaceSchemaLocation> 
     <label>Home Visit Form</label> 
     <type>TextView</type> 
     <name>HomeVisitForm</name> 
    </Field0> 
    <Field1> 
     <id>002</id> 
     <name>Ijas</name> 
     <subjects> 
      <Field0>Science</Field0> 
      <Field1>History</Field1> 
      <Field2>Social</Field2> 
     </subjects> 
    </Field1> 
</Form> 

回答

0

嗨,你可能需要查看该位

(is_array($value))    
      ? array_to_xml($value, $xml_Form->addChild("$key")) 
      : $xml_Form->addChild("$key","$value"); 

这可能是正确的,但目前还不清楚它的写法。通常人们会赋值等

$var = (is_array($value))    
      ? array_to_xml($value, $xml_Form->addChild("$key")) 
      : $xml_Form->addChild("$key","$value"); 

或者

if(is_array($value)){   
    array_to_xml($value, $xml_Form->addChild("$key")); //recursive 
}else{ 
    $xml_Form->addChild("$key","$value"); 
} 

它可能无法帮助你,但只是看起来对我不好。此外,但更重要的是,您正试图设置属性name="DueDate"为例。但是,在代码中的任何地方我都看不到$xml_Form->addAttribute()的调用。

http://php.net/manual/en/simplexmlelement.addattribute.php

可能你需要这样的事情,但我现在不能测试。

foreach($Form as $key => $value) { 
    $key = is_numeric($key) ? "Field$key" : $key; 
//.... 

将上面的内容更改为这样的内容。

$child = $xml_Form->addChild("Field"); 
foreach($Form as $key => $value) { 
    $child->addAttribute($key, $value); 
} 

可能需要一些工作弄清楚如何处理这部分虽然

'subjects' => array('Science','History','Social') 
+0

您好我有一个错误PHP的警告做:的SimpleXMLElement ::的addAttribute()预计参数2为字符串。在'科目'=>数组('科学','历史','社会')我认为这是一个领域的孩子 – user4212650 2014-11-04 07:38:28

+0

我想我提到需要一些计算工作。对不起,这里晚了,我很困。 – ArtisticPhoenix 2014-11-04 08:16:27