Weblog of Abdullah Rubiyath :: Alif

Exploring the Web: An apple a day

PHP 5 XMLReader: Reading XML with Namespace (Part 2)

Tweet
Wed, 04/08/2009 - 18:00 -- alif

This is a continuation of previous article, where I wrote on how to use PHP'5 DOM to read XML Files easily. But, for large files, its better to use XMLReader. Unlike DomDocument, XMLReader does not load the entire File on memory. It reads an XML file one node at a time. I hardly use XMLReader because it requires writing a lot of codes, but for extremely large XML Files, its better to use it. Full reference on XMLReader can be viewed here. It requires PHP 5.2.

The following are the functions I use:

  1. open - opens XML File
  2. read - reads node of XML
  3. getAttribute - gets the attribute of a node
  4. moveToNextAttribute - moves to next Attribute
  5. next - moves to next node

Here's a Basic XML File called 'test.xml':



	
		SCJP 1.5
		
	
        
		jQuery How To
		
	

Below is a way to read it:

At first initialize and open the XML file

$xmlReader = new XMLReader();
// open the file for reading
$xmlReader->open('test.xml')

Now keep reading nodes until the end has been reached, which done by a while loop:

while($xmlReader->read()) { 

}

Below is the full code:

$bookList = array();
$i=0;
$xmlReader = new XMLReader();
$xmlReader->open('test.xml');
while($xmlReader->read()) {
        // check to ensure nodeType is an Element not attribute or #Text  
	if($xmlReader->nodeType == XMLReader::ELEMENT) {
		if($xmlReader->localName == 'book') {
			$bookList[$i]['book_isbn'] = $xmlReader->getAttribute('isbn');
		}
		if($xmlReader->localName == 'name') {
			// move to its textnode / child
			$xmlReader->read(); 
			$bookList[$i]['name'] = $xmlReader->value;
		}
		if($xmlReader->localName == 'info') {
			// move to its textnode / child
			$xmlReader->read(); 
			$bookList[$i]['info'] = $xmlReader->value;
			$i++;
		}
		
	}
}

Here's a var_dump of $bookList

array(2) {
  [3]=>
  array(3) {
    ["book_isbn"]=>
    string(3) "781"
    ["name"]=>
    string(8) "SCJP 1.5"
    ["info"]=>
    string(34) "Sun Certified Java Programmer book"
  }
  [4]=>
  array(3) {
    ["book_isbn"]=>
    string(3) "194"
    ["name"]=>
    string(18) "jQuery is Awesome!"
    ["info"]=>
    string(21) "jQuery Reference Book"
  }
}

That's about it. It requires writing a lot of codes, but it's useful for large (by large I mean extremely large) XML files.

However, when the XML files is very complex (and not extremely large), I find both DomDocument or XMLReader are not ideal solution. I rather use XPath (DomXPath), which I will hopefully write in my next article.

Attachment Size
test.xml 300 bytes
xmlreader_test.php.txt 677 bytes

Comments

Parsing Media RSS

Submitted by Anonymous (not verified) on

Hello I find your code very useful.. What would you recommend for me if I am going to parse a huge XML? I try using DOM and It really eats a lot of memory, I got a memory error. I believe XMLReader is okay like your code above, does it support parsing media RSS? Because in my RSS I have this value inside the node

U can DOM object for small

Submitted by deepak (not verified) on

U can DOM object for small files and XML reader for huge files

Hello I find your code very

Submitted by Anonymous (not verified) on

Hello I find your code very useful.. What would you recommend for me if I am going to parse a huge XML? I try using DOM and It really eats a lot of memory, I got a memory error. I believe XMLReader is okay like your code above, does it support parsing media RSS? Because in my RSS I have this value inside the node

Yes, it can be done, RSS is also a type of XML file

Submitted by alif on

Hi,

yes, it can definitely be done. I received your email, so I will explain it on the email.

Thank you for sharing your

Submitted by Karel (not verified) on

Thank you for sharing your code, I found it very useful.
Karel

Hello may be some body can

Submitted by PhP_Newuser (not verified) on

Hello

may be some body can help me!

i need to bring the values (ref) of IfcObjectPlacement, IfcProductRepresentation, there is more than one IfcFlowSegment in my xml file and i need to bring them all and process them further.
below is my code- IT IS NOT WORKING; IT BRING JUST ONLY IFCFLOWSEGEMENT ID.

how i can delete the ifcflowsegement that i find from the xml file.

how i can stop the reader and start from the begining again

Pipe Types:PVC - Sanitary:546553

Pipe Types:PVC - Sanitary

$xmlReader = new XMLReader();
$xmlReader->open('512M.xml');
while($xmlReader->read()) {

// check to ensure nodeType is an Element not attribute or #Text

if($xmlReader->nodeType == XMLReader::ELEMENT) {

if($xmlReader->localName == 'IfcFlowSegment') {

$IfcFlowSegment_ID= $xmlReader->getAttribute('id');
echo $IfcFlowSegment_ID; echo "";
}

if($xmlReader->localName == 'IfcObjectPlacement') {

// move to its textnode / child

$xmlReader->read();

$IfcObjectPlacement_Ref = $xmlReader->value;
echo $IfcObjectPlacement_Ref ; echo "";
}

if($xmlReader->localName == 'IfcProductRepresentation') {

// move to its textnode / child
$xmlReader->read();

$IfcProductRepresentation = $xmlReader->value;

echo $IfcProductRepresentation; echo ""; echo "";

}
}
}

Thank you and looking for your feedback

I'm using xmlreader to load

Submitted by Roberto (not verified) on

I'm using xmlreader to load an xml file like:

And all is OK!

But now i need to write many times the xml and append elements... I have a problem with the !!!!!!!!!!!!

without should be perfect, i could simply append using php functions:

...

But without xmlreader give error at opening!!

How can i read using xmlreader an xml ???
or
How can i write an xml between the last element and the ???
Please help

please help me out. i want to

Submitted by Aamir (not verified) on

please help me out. i want to read the below file using php. the file is of very big size (in GBs). Please help me out as i dont knw much about this. how to deal with namespaces??

Here is the file

info about article

title here
author 1
author 2
author 3
author n

subject here

resume infor

Add new comment