0

I have read one of the question here in stackoverflow and one of the user share this link by @Mifas and @Abhishek Madhani and this is working.

Now, I want to format the xml output like this:

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

But I don't know how.

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

?>

Output of the PHP code above:

<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>
Community
  • 1
  • 1
user4212650
  • 23
  • 1
  • 8

1 Answers1

0

Hi you might want to review this bit

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

It might be correct but it's not clear the way it's written. Typically one would assign the value etc.

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

Or

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

It may not help you but just looks bad to me. Also, but more importantly you are trying to set attributes name="DueDate" for example. However, I don't see a call to $xml_Form->addAttribute() anywhere in your code.

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

probably you need something like this, but I cant test right now.

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

Change the above to something like this.

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

Might take some work to figure out what to do with this part though

 'subjects'  => array('Science','History','Social')
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Hi I have an error PHP Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string. In 'subjects' => array('Science','History','Social') I thinks that's for a child of a field – user4212650 Nov 04 '14 at 07:38
  • I think I mentioned that it would take some figuring to work that in. Sorry it's late here and I am sleepy. – ArtisticPhoenix Nov 04 '14 at 08:16