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 <SpecialParameter> and the number in <SpecialInfo> 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 <book> has some text inside the book tag like <book isbn="0U812"><title></title><author></author></book> how do you pull out the isbn?

Thanks!

Member Avatar for iamthwee

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

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.

bump

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

commented: What the hell? -3

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

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

The <SpecialParameter> node contains a 'type' which dictates what further processing must occur and the outter <book> 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!

$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.

commented: you beat me to it +15
Member Avatar for iamthwee

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 legit

test.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 '<br>';
echo  $blah;


?>

Output

Some Text
123456789
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.