Parsing XML with PHP menu

User Tag List

Results 1 to 9 of 9
  1. #1
    NerieX's Avatar Master Sergeant
    Reputation
    40
    Join Date
    Jul 2010
    Posts
    110
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing XML with PHP

    Hello MMOwned.
    In this guide i will learn you how to parse XML in a simple and easy way with PHP!

    Whats recommended to know:
    Simple PHP knowledge
    Simple HTML knowledge

    Okay first we will make our XML file.
    We will just put some random things in it like this:

    example.xml:
    Code:
    <person>
        <firstname>Edward</firstname>
        <lastname>Johnson</lastname>
        <age>78</age>
        <email>[email protected]</email>
        <username>Edwii</username>
        <password>edward123</password>
    </person>
    Please note this is only for examples! I don't know if any user here has the username Edwii xD

    Now lets start on the PHP.
    First we would of course make the
    Code:
    <?php
    ?>
    thats pretty logic right? xD

    now what to do? :'(
    now lets load the XML file!
    for this we will use a function named simplexml_load_file()

    we're gonna make our own function!
    Now lets continue.
    Lets make our function
    to make functions (its pretty easy) we simply do like this:
    Code:
    <?php
    function YourFunction() {
    }
    ?>
    Now we won't name our function YourFunction.
    We will name it XMLoad

    Code:
    <?php
    function XMLoad() {
    }
    ?>
    but there is something wrong.
    What is wrong?
    Yep thats right.
    The functions does absolutely NOTHING!
    And to make it possible to load XML we will have to add a parameter to the function.
    Like this:
    Code:
    <?php
    function XMLoad($xmlfile) {
        echo $xmlfile;
    }
    ?>
    Now try and do like this under the function:

    Code:
    XMLoad("heey");
    wow it writes heey on the page.
    Now you got a working function.
    Now lets make it work with loading xml!
    The function needs to look like this:
    Code:
    <?php
    function XMLoad($xmlfile) {
        $xml = @simplexml_load_file($xmlfile);
        if(!$xml) {
            echo "Unable to load XML File!";
        }
        else {
        }
    }
    ?>
    now the XMLoad function can load a XML file but it does not output anything. Is that wierd or wierd? NOT AT ALL!
    To make it display we will have to do like this:
    Code:
    $xml->person->firstname.
    pretty hard huh?
    now lets make the function print all of the things in the XML file
    Code:
    <?php
    function XMLoad($xmlfile) {
        $xml = @simplexml_load_file($xmlfile);
        if(!$xml) {
            echo "Unable to load XML File!";
        }
        else {
            echo "<h1>Person Info</h1>First name: " . $xml->person->firstname . "<br>
            Last name: " . $xml->person->lastname . "<br>
            Age: " . $xml->person->age . "<br>
            E-mail: " . $xml->person->email . "<br>
            Username: " . $xml->person->username . "<br>
            Password: " . $xml->person->password . "<br>";
        }
    }
    ?>
    now the function is finished.
    Now we only need to do one thing.
    Save the function as person.php
    now make a new php file named index.php
    and in that file write this:
    Code:
    <?php
    include("person.php");
    XMLoad("example.xml");
    ?>
    Now it should echo all the information of the person Edward Johnson if just you saved the example XML file as example.xml

    Thats was all of this tutorial.

    Greetings, NerieX.

    Parsing XML with PHP
  2. #2
    Danne206's Avatar Contributor
    Reputation
    183
    Join Date
    Jan 2008
    Posts
    717
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're going to parse XML, you SHOULD know how to create a function .

    Unless you use it several times, there's really no reason to create a function for it.
    else {
    }

    Big nono. Why would you do that? I can't see any reason.
    Code:
    if(true) {
    //Do something
    } else {
    //Do nothing
    }
    is equal to

    Code:
    if(true) {
    //Do something
    }
    EDIT: +4 for contributing!
    Dahnniel [DOT] s [AT] gmail [DOT] com

  3. #3
    Krillere's Avatar Contributor
    Reputation
    112
    Join Date
    Nov 2007
    Posts
    668
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Danne206 View Post
    If you're going to parse XML, you SHOULD know how to create a function .

    Unless you use it several times, there's really no reason to create a function for it.
    else {
    }

    Big nono. Why would you do that? I can't see any reason.
    Code:
    if(true) {
    //Do something
    } else {
    //Do nothing
    }
    is equal to

    Code:
    if(true) {
    //Do something
    }
    EDIT: +4 for contributing!
    I'm pretty sure that you can't give more than 1 rep at a time now.

  4. #4
    The-Eradicator's Avatar Contributor

    Reputation
    149
    Join Date
    May 2007
    Posts
    829
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Krillere View Post
    I'm pretty sure that you can't give more than 1 rep at a time now.
    I believe you still have rep power, you just can't decide how much you give. So if you have 4 rep power, you give 4 rep. But you cannot give say, 2 rep anymore.
    The most beautiful thing we can experience is the mysterious. It is the source of all true art and all science. He to whom this emotion is a stranger, who can no longer pause to wonder and stand rapt in awe, is as good as dead: his eyes are closed.
    Albert Einstein

  5. #5
    NerieX's Avatar Master Sergeant
    Reputation
    40
    Join Date
    Jul 2010
    Posts
    110
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not a rep-chat thread

    Ty danne :P
    But the if else thingy is used in this function... look at the finished script

  6. #6
    Danne206's Avatar Contributor
    Reputation
    183
    Join Date
    Jan 2008
    Posts
    717
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by NerieX View Post
    This is not a rep-chat thread

    Ty danne :P
    But the if else thingy is used in this function... look at the finished script
    Oh, my bad! I just looked through it quick - apologizes!:]
    Dahnniel [DOT] s [AT] gmail [DOT] com

  7. #7
    Allstar .ιllιlı.'s Avatar Banned
    Reputation
    284
    Join Date
    Jan 2009
    Posts
    481
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks man! Just what I was looking for
    +Rep

  8. #8
    Dombo's Avatar Banned
    Reputation
    622
    Join Date
    Nov 2008
    Posts
    1,421
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very decent and comprehensible tutorial

  9. #9
    NerieX's Avatar Master Sergeant
    Reputation
    40
    Join Date
    Jul 2010
    Posts
    110
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe ty :P

Similar Threads

  1. Awesome guide for starting with PHP!
    By Traz in forum Programming
    Replies: 9
    Last Post: 12-02-2008, 02:43 PM
  2. Phisher made with php
    By darkninja in forum Programming
    Replies: 4
    Last Post: 04-21-2008, 10:05 PM
  3. Help with PHP
    By mkultra. in forum Community Chat
    Replies: 4
    Last Post: 12-31-2007, 08:48 AM
All times are GMT -5. The time now is 04:33 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search