954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP DOM with XML

Hey All!

I'm having trouble getting the nodeValue of a specific set of nodes. The xml looks like this:

<root>
<SpecialParameter>Some Text</SpecialParameter>
<SpecialInfo someNumber="123456789"></SpecialInfo>
<book>
<title>Rusty Bedsprings</title>
<author>I. P. Knightly</title>
</book>
<book>
<title>Rush To The Out House</title>
<author>Willie Makeit</title>
</book>
</root>

What I need to get are the and the number in so what I have is this:

$sp = $doc->getElementsByTagName( "SpecialParameter" );
  $directive = $sp->item(0)->nodeValue;
  $si = $doc->getElementsByTagName( "SpecialInfo" );
  $directive = $si->item(0)->nodeValue;


Both return objects but the objects are empty... What's the secret to accessing the first two nodes? Or, for that matter if has some text inside the book tag like how do you pull out the isbn?

Thanks!

ppetree
Junior Poster
158 posts since Oct 2009
Reputation Points: 12
Solved Threads: 8
 

Any particular reason why you're not using php 5 simplexml, it's just so much easier to work with IMO.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
Any particular reason why you're not using php 5 simplexml, it's just so much easier to work with IMO.


Because the original work was done using DOM and the spec has changed and I need to support the change without rewritting the entire 1000 lines of code.

ppetree
Junior Poster
158 posts since Oct 2009
Reputation Points: 12
Solved Threads: 8
 

bump

ppetree
Junior Poster
158 posts since Oct 2009
Reputation Points: 12
Solved Threads: 8
 

hey dude It seems to me that your problem is somehow to be solved but not that kinda sort of help.... your script in java script getElementByTagName get's only the tagname that converts into an object but not getting the tagname's element you have also to fetch data from the tagname.

try the getElementById and in every tag of yours like the book tag or what is to put the id name inside the tag.

the problem is you need to create a program counter for script tag book tag and special parameter tag.

for instance a script tag itself has exact number of 1234 then counter will release 1234 script tags and inside the counter is having the special parameter and the book tag which has also 1234 repeated tags for both special paramter and book tag.

I imagined it this way
For loop will get the total number of script tags.... inside counter was the counter for special parameter tag and the book tag. now if you get the program counter correctly then your in the halfway to solve it because

you can have multiple getElementById in every tags. e.g.

//get first the number of special prameter tag that is repeated
for (x=1;x<=1234; x++)
{
$idElementForSpecialParameter=SpecialParameter[x]
$idElementNameForBook=book[x];
$idElementNameForSpecialParamater=specialInfo[x];

//insertint the counter value
echo"<SpecialParameter getElementById="./*program counter gets here*/$idElementForSpecialParameter.">Sometext </SpecialParameter>";
echo"SpecialInfo getElementById="./*program counter gets here*/$idElementForSpecialInfo." someNumber=\"123456789\"></SpecialInfo>
echo<book getElementById="./*program counter gets here*/$idElementForBook.">
  <title>Rusty Bedsprings</title>";

as u can see in this manner a counter gets in the element of the Id.

in java script function will have pass the vaalue get by the for loop an do some srot of a thing there.

"think this way... imagine you in a database compiler... :)" hope this helps for you some how

masterjiraya
Junior Poster
153 posts since Jul 2008
Reputation Points: 10
Solved Threads: 4
 
Because the original work was done using DOM and the spec has changed and I need to support the change without rewritting the entire 1000 lines of code.


Then rewrite those functions/classes using simpleXML

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

There are 1 and only 1 node and 1 and only 1 node and there may be 1000's of "book" nodes and each of them may have 100's of nodes.

The node contains a 'type' which dictates what further processing must occur and the outter node gets passed to one of three sub-routines based on that 'type'. What I was trying to do was avoid looping through possibly 100's of 1000's of nodes just to get the contents of two nodes just to pass it off to a function that would loop through all those nodes again.

Rewriting it would mean rewriting 1000's of lines of code as well... plus all the testing, close down, redeployment... it would add months to a project that just needs two simple parameters!

ppetree
Junior Poster
158 posts since Oct 2009
Reputation Points: 12
Solved Threads: 8
 
$doc = new DOMDocument();
$doc->loadXML('Your XML string');

//Gets the SpecialParameter node value
$specialParameter = $doc->getElementsByTagName('SpecialParameter')->item(0)->nodeValue;

//Gets the SpecialInfo::someNumber attribute value
$specialInfo = $doc->getElementsByTagName('SpecialInfo')->item(0)->getAttribute('someNumber');


This is untested but I believe this should get you in the right direction at the minimum.

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

test.xml

<root>
<SpecialParameter>Some Text</SpecialParameter>
<SpecialInfo someNumber="123456789"></SpecialInfo>
<book>
  <title>Rusty Bedsprings</title>
  <author>I. P. Knightly</author>
</book>
<book>
  <title>Rush To The Out House</title>
  <author>Willie Makeit</author>
</book>
</root>


I cleaned up your sample xml file so it is legittest.php

<?php
$doc = new DOMDocument();
  $doc->load( 'test.xml' );



$sp = $doc->getElementsByTagName( "SpecialParameter" );
  $directive = $sp->item(0)->nodeValue;
  
  
  $si = $doc->getElementsByTagName( "SpecialInfo" );
  $blah = $si->item(0)->getAttribute('someNumber');

 
echo $directive;
echo '';
echo  $blah;


?>


Output

Some Text
123456789
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: