Partage
  • Partager sur Facebook
  • Partager sur Twitter

result xml without CDATA and the first tag of XML

    2 août 2016 à 12:14:46

    hello ,

    this is my funtion PHP that s return an xml content

    function arrayToXml($array, $rootElement = null, $xml = null) {
      $_xml = $xml;
     
      if ($_xml === null) {
        $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<data/>');
      }
     
      foreach ($array as $k => $v) {
        if (is_array($v)) { //nested array
          arrayToXml($v, $k, $_xml->addChild($k));
        } else {
          $_xml->addChild($k, $v);
        }
      }	
        return $_xml->asXML();
    }

    this is an example of the result 

    <![CDATA[<?xml version="1.0"?>
    <data><0><eligibility>-1</eligibility></0></data>]]>

    i want to don t display the CDATA and the version just tha tags 

    pllllz help me

    -
    Edité par BenProgrammer 2 août 2016 à 12:15:43

    • Partager sur Facebook
    • Partager sur Twitter
      2 août 2016 à 12:36:13

      Somehow you ended up posting this on the french mirror of the site, for some reason... o_O

      Nevertheless, I've always found SimpleXML to be seriously more complicated and bizarre than the supposed-to-be-complecated DOM. At least, with DOM, an apple will always be an apple, if you catch my meaning. ;)

      Here's what your function would look like using DOM

      function arrayToXml(array $array, DOMElement $rootElement = null) {
        if(is_null($rootElement)) {
          $_xml = new DOMDOcument('1.0', 'utf-8'); // version and encoding
          $_xml->appendChild($_xml->createElement('data')); // create root <data/>
          $rootElement = $_xml->documentElement;
        } else {
          $_xml = $rootElement->ownerDocument;
        }
        
        foreach ($array as $k => $v) {
          if (is_array($v)) { //nested array
            $new = $_xml->createElement($k);
            $rootElement->appendChild($new);
            arrayToXml($v, $new);
          } else {
            $new = $_xml->createElement($k, $v);
            $rootElement->appendChild($new);
          }
        }
        return $_xml->saveXML();
      }



      • Partager sur Facebook
      • Partager sur Twitter

      result xml without CDATA and the first tag of XML

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown