Jump to content

Simplexml Paths


RandomClown
 Share

Recommended Posts

Hai!Sorry if this is a noob question [i am a noob]. I am new to PHP & XML.I was trying to echo the value of an XML child, but could not get it to work completely.This is a sample of my XML document:

<Stuff>	<Sec>		<Program>abc 123</Program>	</Sec></Stuff>

This is a sample of code I tried:

	$Section="Sec";	$Page="Program";echo $xml->$Section->Program;		// Works for some reason.echo $xml->$Section->$Page;		// With a 2nd variable, wont work.

Does anyone know what I did wrong?Thanks for reading.

Edited by RandomClown
Link to comment
Share on other sites

I've worked with simpleXML but never needed to do this exact thing, so I tried it. First with one of my files, then with your exact data and code. The thing you want to do worked every time.The only answers I come up with are (1) the snippet you posted is not the code you're actually using (2) you're experiencing some sort of cache problems (3) your version of PHP is not the same as mine and for some reason that makes a difference in this context. I'm not enough of an expert in this module to know its history. FWIW, I'm on 5.2.10

Link to comment
Share on other sites

OMG I Spent an entire 2 days on this!!!!!!!!!!!!!The reason it was working half way was because I cant /GET & in javascript.var S is the section while P was the page. I could pass S, but not P:

open("GET","page.php?s=bla&p=bla",true) // & symbol exploded to & which = fail to pass multi vars.

open("GET","page.php?s=bla&p=bla",true) // plain & symbol which XHTML hates.

now I am trying to make my page completely [XHTML 1.1] compatible [cause I want to :)]but the problem with this is I cannot use & in urls.Does anyone know of a way to send URLs?I saw POST as an alternative, but I dont want a form in my page.

Link to comment
Share on other sites

XHTML "hates" the ampersand character because it has a reserved meaning, namely to begin an entity. That is why you have to turn it into an entity itself when you want to use it. However, in JS "&" has no special meaning and can be used freely.If you need to use an ampersand in XML or SGML, just use its entity.

Link to comment
Share on other sites

The best you can do is isolate the JavaScript in its own file. It will be easiest and least painful thing to do. You won't have to do any kind of escaping with this approach.If you want to embed the JavaScript on the page, you'll have to combine several kinds of comments into one another for the sake of different parser types. Namely, you'll have to enclose your code like this:

<script type="text/javascript">/*<![CDATA[*/...open("GET","page.php?s=bla&p=bla",true).../*]]>*/</script>

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...