hello, I need to add items in the 'ide' element, so that it looks like the example below saving in XML (thank you!).

The example I need:

<ide> <cUF>43</cUF> <cCT>00000004</cCT> <CFOP>6353</CFOP>... </ide>

In the current situation, Im getting only <cUF>:

<?xml version="1.0" encoding="UTF-8"?> <CTe xmlns="http://www.portalfiscal.inf.br/cte"> <infCte versao="3.00" Id="CTe43120178408960000182570010000000041000000047"> <ide><cUF>43</cUF></ide> </infCte> </CTe>
array
$data = array(
    'name' => 'CTe', 
    'attributes' => array(
    'xmlns' => '...cte',),array(
        'name' => 'infCte',
        'attributes' => array(
            'versao' => '3.00',
            'Id' => 'CTe43120178408960000182570010000000041000000047',
        ),array(
            'name' => 'ide',
          array(
               'name' => "cUF",
               'value' => '43',   
               'name' => '<cCT>',
               'value' => '00000004',
             ),
          ),
    ),
);

passing data to XML

function generate_xml_element( $dom, $data ) {
if ( empty( $data['name'] ) )
    return false;

// Create the element
$element_value = ( ! empty( $data['value'] ) ) ? $data['value'] : null;
$element = $dom->createElement( $data['name'], $element_value );
  // Add any attributes
if ( ! empty( $data['attributes'] ) && is_array( $data['attributes'] ) ) {
    foreach ( $data['attributes'] as $attribute_key => $attribute_value ) {
        $element->setAttribute( $attribute_key, $attribute_value );

    }
}

// Any other items in the data array should be child elements
foreach ( $data as $data_key => $child_data ) {
    if ( ! is_numeric( $data_key ) )
        continue;

    $child = generate_xml_element( $dom, $child_data );
    if ( $child )
        $element->appendChild( $child );
}
return $element;
}
$doc = new DOMDocument('1.0','UTF-8');
$child = generate_xml_element( $doc, $data );
if ( $child)$doc->appendChild( $child );
$doc->formatOutput = true;
$xml = $doc->saveXML();
$doc->save('contas.xml');

I played around with it a bit, but SimpleXML really needs to change it's name because it's not simple to work with at all. I might take another crack at it in a little bit.

That being said, is there a reason that you are forced to use XML? PHP makes it sooooo easy to work with the JSON format and JSON is even more portable than XML is.

You can literally just do: $output = json_encode($data, JSON_PRETTY_PRINT) to nicely format $output into a JSON text string.

Hi Dani, many thanks for your reply.

my problems is that arrays comes from a sql results...

`

while( $row = odbc_fetch_array($result) ) {

     $adicao = $row ['adicao'];

$data = array(
    'name' => 'CTe', // "name" required, all else optional
    'attributes' => array(
    'xmlns' => 'http://www.portalfiscal.inf.br/cte',
   ), 
       array(
        'name' => 'infCte',
        'attributes' => array(
            'versao' => '3.00',
            'Id' => 'CTe43120178408960000182570010000000041000000047',
        ),
       array(
            'name' => 'ide',
             array(
               'name' =>  'cUF',
               'value' => $adicao,

            ), 
         ),
         array(
            'name' => 'compl',
          array(
               'name' => "xEmi",
               'value' => 'Master',           
          ),
         ),
       array(
            'name' => 'emit',
          array(
               'name' => "CNPJ",
               'value' => '78408960000182',            
          ),
         ),

       ),
);

   }

`

So, take the php results to PHP array to json and after that to XML is a little complicated, once I need to add the attributes in some elements. sorry if you don't get what Im trying to explain.

What I'm saying is why do you need XML at all? Why can you just not work with JSON and only JSON? JSON is super easy to convert to and from a PHP array and it also can be stored as a string and outputted in a pretty format. IMHO it has all the benefits of XML and more.

I see. It's just that sometimes when something is more difficult than it should be, there's a way to somehow work around dealing with it.

The problem is I converted your PHP array to JSON using the json_encode() function and then ran it through a few different JSON to XML php parsers, and it broke in all of them.

e.g. https://github.com/mevdschee/json2xml.php

The reason I converted it to JSON first is because all of the PHP array to XML parsers I found on the web didn't work and some of the JSON to XML parsers seemed more robust.

hi Dani, It's solved!!
thank you!!

Glad to hear!! How did you do it? Mind sharing so that it could be helpful to others in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.