/    Sign up×
Community /Pin to ProfileBookmark

newb XML question

Greetings,

I’m an expert when it comes to anything regarding ASP, but I’m currently using a web host that only supports php (:cough:yahoo:cough?, and lucky me I’ve never used the language.

I’m sending variables to a php page in this form:

Map.php?map=****&id=****&path=****&x=****&y=****

I assume thats the correct way to do it,

now in the actual php page I’d like to grab those variables and put them in an XML file but I’m not confident enough in the syntax of this language and its XML parsers to do it:

[code=php]<?php
//Get variables passed to this page?
icon=$_GET[‘id’];
map=$_GET[‘id’];
path=$_GET[‘id’];
x=$_GET[‘id’];
y=$_GET[‘id’];

//open the xml database
$doc = new DomDocument;
$doc->Load(‘blah/guilds.xml’)

?>[/code]

From this point I’m a little stuck, how do I update XML nodes from this point? And if they don’t exist, create them?

Heres the format of my XML doc:

[code=php]<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<guilds>
<icon id=”####”>
<map>???</map>
<path>http://blah</path>
<x>100</x>
<y>100</y>
</icon>
<icon id=”####”>
<map>???</map>
<path>http://blah2</path>
<x>200</x>
<y>200</y>
</icon>
</guilds>[/code]

Any help you can provide would be greatly appreciated.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 30.2006 — I'm guessing this is probably a typo:
[code=php]
//Get variables passed to this page?
icon=$_GET['id'];
map=$_GET['id'];
path=$_GET['id'];
x=$_GET['id'];
y=$_GET['id'];
[/code]

But just in case, it should be something like:
[code=php]
//Get variables passed to this page?
icon=$_GET['id'];
map=$_GET['map'];
path=$_GET['path'];
x=$_GET['x'];
y=$_GET['y'];
[/code]
Copy linkTweet thisAlerts:
@NogDogNov 30.2006 — PS: You might want to look at the [url=http://www.php.net/simplexml]SimpleXML[/url] functions.
Copy linkTweet thisAlerts:
@TNOauthorNov 30.2006 — Thanks for the advice, but my server is stuck on PHP 4.3.0, so SimpleXML isn't an option according to the webhost's guide on the topic.

Heres how I would generally do it in javascript:

[code=php]
// Load XML
var mapxml = new ActiveXObject("MSXML2.DOMDocument.3.0");
mapxml.load("map.xml");

//Get and Test if the specific icon node exists
var test = mapxml.selectSingleNode("guildsicon[@id="+id+"]");

//If it does exist
if (test != null){
//update the properties
test.childNodes.item(0).text=map;
test.childNodes.item(1).text=path;
test.childNodes.item(2).text=x;
test.childNodes.item(3).text=y;
//save the file
mapxml.save("map.xml");
}
//If it does not exist, create it
else{
//define the document base tag
var guilds = mapxml.documentElement;
//create <icon> element
var icon1 = mapxml.createNode(xmlNodeType.Element, "icon", "");
//create the id for the element
var iconid = mapxml.createAttribute("id")
//set the value for the attribute
iconid.value = id;
//add the attribute to the tag
icon1.attributes.setNamedItem(iconid)
//append <icon id="blah"> to <guilds>
guilds.appendChild(icon1)

//create <map> element
var map1 = mapxml.createNode(xmlNodeType.Element, "map", "");
//create the text node
var map1value=mapxml.createTextNode(map)
//append the text to the element
map1.appendChild(map1value)
//append <map>blah</map> to <icon>
icon1.appendChild(map1)

//create <path> element
var path1 = mapxml.createNode(xmlNodeType.Element, "path", "");
//create the text node
var pathtext = =mapxml.createTextNode(path)
//append the text to the element
path1.appendChild(pathtext)
//append <path>blah</path> to <icon>
icon1.appendChild(path1)

//create <x> element
var x1 = mapxml.createNode(xmlNodeType.Element, "x", "");
//create the text node
var xtext = mapxml.createTextNode(x)
//append the text to the element
x1.appendChild(xtext)
//append <x>blah</x> to <icon>
icon1.appendChild(x1)

//create <y> element
var y1 = mapxml.createNode(xmlNodeType.Element, "y", "");
//create the text node
var ytext = mapxml.createTextNode(y)
//append the text to the element
y1.appendChild(ytext)
//append <x>blah</x> to <icon>
icon1.appendChild(y1)

//save the file
mapxml.save("map.xml");
}
[/code]


Can anyone assist with the translation? Or tell me the equivalent functions in XML DOM for PHP?
×

Success!

Help @TNO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...