douxidang9092 2017-04-05 13:56
浏览 27

SimpleXML重新格式化

I am trying to write an API to write data to my POS system from magento. My POS supplier gives me a code on which I need to write the data in XML 1.0 format. I wrote the data using SimpleXML form, as I need to do some loops to get all the data I need.

The problem is that the API is not reading the data I wrote correctly. I've already tried to use DOM to reformat the code without success. How can I edit this SimpleXML so the data I post is as it was XML 1.0.

Apparently what causing the problem is the rawurlencode function that can't read SimpleXML, but my API only understands this way...

I am new to PHP and XML, so any suggestions are welcome. Thanks in advance.

$xml = '<?xml version="1.0" encoding="UTF-8"?><pedido></pedido>';
$xmlpost = new SimpleXMLElement($xml); 
$pedidob = $xmlpost->addChild('pedido');
$clienteb = $pedidob->addChild('cliente');
$clientebno = $clienteb->addChild('nome',$nome);
$clientebcp = $clienteb->addChild('cpf_cnpj',$cpf);
$clienteben = $clienteb->addChild('endereco',$rua);
$clientebnu = $clienteb->addChild('numero',$numero);
$clientebco = $clienteb->addChild('complemento',$complemento);
$clientebba = $clienteb->addChild('bairro',$bairro);
$clientebce = $clienteb->addChild('cep',$cep);
$clientebci = $clienteb->addChild('cidade',$cidade);
$clientebuf = $clienteb->addChild('uf',$estado);
$clientebfo = $clienteb->addChild('fone',$telefone);
$clientebce = $clienteb->addChild('celular',$celular);
$clientebem = $clienteb->addChild('email',$email);
$itensb = $pedidob->addChild('itens');
    for ($k = 0; $k < $arrayl; $k++) {
            $itemb = $itensb->addChild('item');
            $codigob = $itemb->addChild('codigo', $infopp[$k][0]);
            $descricaob = $itemb->addChild('descricao', $infopp[$k][1]);
            $qtdeb = $itemb->addChild('qtde', $infopp[$k][2]);
            $vlrunitb = $itemb->addChild('vlr_unit', $infopp[$k][3]);
        }
$parcelasb= $pedidob->addChild('parcelas');
$parcelab = $parcelasb->addChild('parcela');
$vlrb = $parcelab->addChild('vlr',$Subtotal);
$freteb = $pedidob->addChild('vlr_frete',$Shipping);

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$dom->loadXML($xmlpost->asXML());
echo $dom->saveXML();

$posts = array (
    "apikey" => "*******",
    "xml" => rawurlencode($xmlpost)
);

</div>
  • 写回答

1条回答 默认 最新

  • duanpao4172 2017-04-05 15:37
    关注

    Ok, this was quite simple actually... I only needed to create another variable and store my SimpleXML asXML() inside it

    $XMLvariable = $simpleXMLvariable-> asXML();

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用