473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

using a variable to get the node number in simpleXML

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
12 3458
Dormilich
8,658 Expert Mod 8TB
it’s probably easiest to do that via XPath: //*[n+1] should give you the nth-node in the document.
Dec 6 '11 #2
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
8,658 Expert Mod 8TB
what do you think?
Dec 6 '11 #4
//child[n]/grandchild[2]

this is assuming first child has n=0

does that seem right?
Dec 7 '11 #5
Dormilich
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
try "//child[$n]/grandchild[2]" for the XPath parameter
Dec 7 '11 #8
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
8,658 Expert Mod 8TB
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

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

Similar topics

5
by: ipg | last post by:
I am running into issues passing a parameter (that is a XML string) into a stylesheet and using msxsl:node-set to parse the string. Can someone please explain why. my html: var xsl = new...
3
by: Andrej Prsa | last post by:
Hello! If I was to write a function that takes variable argument number that would simply call printf with those exact arguments, how do I pass them to printf? E.g. int my_printf (const...
5
by: Andrej Prsa | last post by:
Hi! Why do I get a warning about incompatible pointer type if I try to assign a pointer to the function with variable argument number: int func (int argc, ...) , but everything is ok...
1
by: kelvinweb | last post by:
Hi All, Is it possible to using variable store container.dataitem in ItemTemplate ?? For example. <ItemTemplate> <% Dim x x = container.dataitem("LineNo")
2
by: Tjerk Wolterink | last post by:
I have a variable $value as a parameter in the following template: <xsl:template name="myTemplate"> <xsl:param name="value"/> <xsl:if test="$value"> <xsl:value-of select="$value"/> </xsl:if>...
10
by: CJM | last post by:
I'm trying to call a package/procedure in oracle (from an ASP page) which requires a number of parameters. I have got this working using OO40 but unfortunately the transaction rollback function...
0
by: Jacn | last post by:
Hi! I have a several problem with the oracle connection and ADO, I use a executenonquery for a select sentence and I use two output parameters and two input parameters, when I run the application an...
9
by: Chad | last post by:
This might be a bit vague and poorly worded..... In my program, I handle function failures using fprintf() and exit() like: fprintf(stderr, "malloc failed"); exit(EXIT_FAILURE); There...
1
by: ragonz | last post by:
Hi, i'm kind of newbie in java programming. Now, i'm working on my task, n i hav a problem in using variable from other method. Here goes the code.. import java.awt.*; import java.awt.event.*;...
1
by: smartic | last post by:
how can i assign value to static var inside class using variable variables technique ex: self::$$var = $value;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.