PHP XML Parsing

前端 未结 5 982
心在旅途
心在旅途 2020-12-14 04:55

Which is the best way to parse an XML file in PHP ?

First
Using the DOM object

//code
$dom = new DOMDocument();
$dom->load(         


        
5条回答
  •  感情败类
    2020-12-14 05:20

    I have started to use XMLReader to parse the XML files. After doing a bit of googling around found it the best to way parse XML files as it does not load the whole XML file into memory. Say if suppose my XML files was of 5 MB, while parsing it using XMLReader 5MB of my memory does not get wasted.

    //usage
    $xml = new XMLReader();
    $xml->XML($xmlString);
    while($xml->read)
    {
    if($xml->localName == 'Something') // check if tag name equals something
    {
    //do something
    }
    }
    

    Using XML Reader we can find if the current tag is an opening tag or closing tag and do the needful as required.

提交回复
热议问题