www.webdeveloper.com
Results 1 to 4 of 4

Thread: newb XML question

  1. #1
    Join Date
    Sep 2004
    Posts
    294

    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:

    PHP Code:
    <?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')

    ?>
    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:

    PHP Code:
    <?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>
    Any help you can provide would be greatly appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,387
    I'm guessing this is probably a typo:
    PHP Code:
    //Get variables passed to this page?
    icon=$_GET['id'];
    map=$_GET['id'];
    path=$_GET['id'];
    x=$_GET['id'];
    y=$_GET['id']; 
    But just in case, it should be something like:
    PHP Code:
    //Get variables passed to this page?
    icon=$_GET['id'];
    map=$_GET['map'];
    path=$_GET['path'];
    x=$_GET['x'];
    y=$_GET['y']; 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,387
    PS: You might want to look at the SimpleXML functions.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Sep 2004
    Posts
    294
    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:

    PHP Code:
    // 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("\guilds\icon[@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");

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles