0

I have strange problem with XML parsing.
PHP:

/**
 * Klasa zamiany pliku XML na tablice w PHP
 * Dodałem metode statyczną utf8_for_xml która usuwa krzaki bezpośrednio przy pobieraniu XML'a
 * 
 * XML2Array: A class to convert XML to array in PHP
 *
 * Author : Abdelwahd Hannabou
 *
 * Usage:
 *       $Array = XML2Array::CreateArray('FILE_PATH');
 */

class XML2Array
{
    public static function utf8_for_xml($string)
    {
        return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $string);
    }

    public static function ampersandClean($string)
    {
       return preg_replace('/&/', '&', $string);
    }

    public static function CreateArray($XmlFilePath)
    {
        if (!file_exists($XmlFilePath)) {
            echo "Wrong path";
            return FALSE;
        }
        $Class = array();
        $Class['UsersFileXml'] = self::ampersandClean(self::utf8_for_xml(file_get_contents($XmlFilePath)));
/*      echo "<pre>";
        print_r($Class['UsersFileXml']);
        echo "</pre>"; */

        $Class['FileXml'] = simplexml_load_string($Class['UsersFileXml']);
        echo "<pre>";
        print_r($Class['FileXml']);
        echo "</pre>";
        $Class['FileJson'] = json_encode($Class['FileXml']);
        $Array = json_decode($Class['FileJson'],TRUE);
        unset($Class);

        return $Array;
    }
}

$xml = XML2Array::CreateArray('p.xml');
/* echo '<pre>';
print_r($xml);
echo '</pre>';
 */

XML p.xml:

<?xml version="1.0" encoding="UTF-8"?>
<main>
<wezel1>test</wezel1>
<wezel2>test2</wezel2>
<something>string so array did not show this what is in "somethingelse"<somethingelse>test3</somethingelse></something>
</main>

Problem is that in array is not showing up node <somethingelse>test3</somethingelse>. How to skip this string and parse next node? When string is not on the first position it is ok:

itwork.xml

<?xml version="1.0" encoding="UTF-8"?>
<main>
<wezel1>test</wezel1>
<wezel2>test2</wezel2>
<something><somethingelse>test3</somethingelse>String but here string is not problem, PHP parse next node<itisok>This is in array</itisok></something>
</main>
  • You should not look for an array in the first place but just parse the XML. PHP comes with three XML parsing libraries that are enabled by default. Otherwise you need to troubleshoot your code and find out why *exactly* it doesn't work first: What is the line in your code where you for the first time get an unexpected value while the line before it still was OK. Have you triple checked it? Where are your tests? – hakre Jan 12 '15 at 10:10
  • I did not found an answer for my question. I parse above XML code using simplexml_load_string, and in above code named p.xml I do not get in any way test3. How to get this if before this node is string? Just create those files and you will see. I described it how I can. – user2536914 Jan 12 '15 at 17:59
  • For **SimpleXMLElement** the examples on how to access elements are outlined in this manual section: [Basic SimpleXML usage](http://php.net/manual/en/simplexml.examples-basic.php) - please let me know if there is an uncertainty on how to access this (and every other element) in the document with simplexml after following those examples. – hakre Jan 12 '15 at 20:37

1 Answers1

-1

This might not be the answer you're looking for, but it's the right one. Don't reinvent the wheel PHP already handled this Check the first comment - I'll save you the trouble: Three line xml2array:

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
Bryan
  • 3,449
  • 1
  • 19
  • 23
  • You are right, but I did it above, my problem is because simplexml_load_string can't skip string before test or I use it in wrong way – user2536914 Jan 11 '15 at 08:51