You are hereBlogs / alif's blog / PHP 5 XMLReader: Reading XML with Namespace (Part 2)

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


By alif - Posted on 08 April 2009

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':

<?xml version="1.0" encoding="ISO-8859-1"?>

	
		SCJP 1.5
		
	
	
		jQuery is Awesome!
		
		

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.

AttachmentSize
test.xml300 bytes
xmlreader_test.php.txt677 bytes
Tags

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

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

info about article

title here
author 1
author 2
author 3
author n

subject here

resume infor

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

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

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

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

Hi,

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

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 files and XML reader for huge files

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.