SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Location
    Jamaica
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML anyone: How do I extract data from an XML file?

    ok first my disclaimer: I am a novice I am therefore not responsible for my asking what may seem to you gurus as the 'obvious'. That said here's the pickle:

    I have a desktop application that spits out an xml file and I just want to extract data from two nodes of that xml file which that has multiple nodes and stores the customer contact information and notes. For each customer I would like to extract the customer name and the corresponding notes and display that on a website. Any information or resources would be greatly appreciated.

    The core of my question is really how do I use PHP read the xml file and extract the data from the specific nodes? Thanks in advance

  2. #2
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a little piece of php I was playing with previously:

    PHP Code:
    <?php

    $doc 
    = new DomDocument;
    $doc->Load("articles.xml");

    $articles $doc->getElementsByTagName('article');

    foreach(
    $articles AS $node)
    {
        
    $cat $node->getAttribute('cat');
        
    $title $node->getElementsByTagName('title')->item(0)->nodeValue;
        
    $author $node->getElementsByTagName('author')->item(0)->nodeValue;
        
    $text $node->getElementsByTagName('text')->item(0)->nodeValue;
        print 
    "<h3>$title</h3>";
        print 
    "<h5>$cat - $author</h5>";
        print 
    "<p>$text</p><hr />";
    }

    ?>
    Also, look at this:

    PHP Code:
    <?php

    $dom 
    = new DomDocument;

    $dom->Load('articles.xml');

    $dom domxml_open_mem($articles);

    $root $dom->document_element();

    function 
    process_node($node)
    {
        if(
    $node->has_child_nodes())
        {
            foreach(
    $node->child_nodes() AS $n)
            {
                
    process_node($n);
            }
        }

        if(
    $node->node_type() == XML_TEXT_NODE)
        {
            
    $content rtrim($node->node_value());
            if(!empty(
    $content))
            {
                print 
    "$content\n";
            }
        }

    }

    process_node($root);

    ?>
    Look up those functions on php.net for more in depth explanations.
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    There is also SimpleXML
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Member
    Join Date
    Sep 2007
    Location
    Jamaica
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Possibility I cant believe its that simple the first code snippet is exactly what I needed. I modified it to look like this:

    $doc = new DomDocument;
    $doc->Load("test.xml");

    $Contact = $doc->getElementsByTagName('Contact');

    foreach($Contact AS $node)
    {
    $name = $node->getAttribute('Name');
    $notes = $node->getElementsByTagName('Notes')->item(0)->nodeValue;
    print "<h3>$name</h3>";
    print "<p>$notes</p>";
    print "<hr />";
    }

    ?>

    ...and weeks of slowly degrading self confidence was just a painful memory, I hope you find all your answers too. Thanks much.


Bookmarks

Posting Permissions

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