0

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!

5
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by iamthwee
0

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.

-1

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

Edited by masterjiraya: more explanation

Votes + Comments
What the hell?
0

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

0

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!

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

Votes + Comments
you beat me to it
0

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

Edited by iamthwee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.