By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

using a variable to get the node number in simpleXML

P: 25
how does one refer to a node in XML when the node number is a variable?

name[0] will return the value of first node with the name "name"

how would I refer to the value of n-th node?

or is it not possible to make a variable reference to a node in xml in php?

thanks much
Dec 6 '11 #1
Share this Question
Share on Google+
12 Replies


Dormilich
Expert Mod 5K+
P: 8,639
itís probably easiest to do that via XPath: //*[n+1] should give you the nth-node in the document.
Dec 6 '11 #2

P: 25
thanks D.. so if the structure of my XML is

Expand|Select|Wrap|Line Numbers
  1. <parent>
  2. <child1> 
  3. <grandchild1> abc </grandchild1>
  4. <grandchild2> def </grandchild2>
  5. </child1>
  6. <child2>
  7. <grandchild1> 123 </grandchild1>
  8. <grandchild2> 456 </grandchild2>
  9. </child2>
  10. </parent>
  11.  
how would you access the value of the second grandchild of the nth child?

thanks much!
Dec 6 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
what do you think?
Dec 6 '11 #4

P: 25
//child[n]/grandchild[2]

this is assuming first child has n=0

does that seem right?
Dec 7 '11 #5

Dormilich
Expert Mod 5K+
P: 8,639
nearly. the problem is that your child and grandchild nodes are named differently, so it would be //childn/grandchild2. from this it can be seen that the XML structure is inefficient, it would be better to name the nodes <child> and <grandchild>, so that you can use indices.
Dec 7 '11 #6

P: 25
yes i made the mistake in my description of the XML file..what i had in mind was a structure like what you said, ie

Expand|Select|Wrap|Line Numbers
  1. <parent>
  2. <child> 
  3. <grandchild> abc </grandchild>
  4. <grandchild> def </grandchild>
  5. </child>
  6. <child>
  7. <grandchild> 123 </grandchild>
  8. <grandchild> 456 </grandchild>
  9. </child>
  10. </parent>
  11.  
let's say i want to get and print the value of the 2nd grandchild of the child $n .. where $n is defined elsewhere (let's say it comes from form input)

this is the code i'm trying to use

Expand|Select|Wrap|Line Numbers
  1. $namelist= simplexml_load_file('myfile.xml'); //myfile.xml is the xml file i am working with 
  2. $result = $namelist->xpath('child[$n]/grandchild[2]'); 
  3. echo $result;
  4.  
and it doesn't work
Dec 7 '11 #7

Dormilich
Expert Mod 5K+
P: 8,639
try "//child[$n]/grandchild[2]" for the XPath parameter
Dec 7 '11 #8

P: 25
thanks much!! the problem was somewhere else entirely - turns out that the variable i was pulling in from a FORM came in as a string and after i converted it into an integer everything worked perfectly

i feel a bit dumb..but hey, 2 weeks ago i knew nothing about coding and here i am talking about XPath .. lol
Dec 7 '11 #9

Dormilich
Expert Mod 5K+
P: 8,639
i feel a bit dumb..but hey, 2 weeks ago i knew nothing about coding and here i am talking about XPath .. lol
it’s all a matter of basics. once you understand these you wonder what took you so long understanding it in the first place.

PS. the fun starts with namespaces
Dec 7 '11 #10

P: 25
the frustrating thing is that for something this basic (ie, php data pulled in from a form via POST won't automatically work as an index number because it needs to be converted into an integer first) you would expect it to be spelled out somewhere and yet i basically had to figure that out on my own .. i have yet to find one reference source that is comprehensive about all these things // *whine*
Dec 8 '11 #11

Dormilich
Expert Mod 5K+
P: 8,639
input validation can take care of that:
Expand|Select|Wrap|Line Numbers
  1. var $n = filter_input(INPUT_POST, "param_name", FILTER_VALIDATE_INT, array("options" => array("min_range" => 1)));
returns null/false on error and an integer otherwise.
Dec 8 '11 #12

Dormilich
Expert Mod 5K+
P: 8,639
input validation can take care of that:
Expand|Select|Wrap|Line Numbers
  1. var $n = filter_input(INPUT_POST, "param_name", FILTER_VALIDATE_INT, array("options" => array("min_range" => 1)));
returns null/false on error and an integer otherwise.
Dec 8 '11 #13

Post your reply

Sign in to post your reply or Sign up for a free account.