XML Question

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Hi, I'm testing with XML and XML parsing right now, and here's what I came up with.

text.xml
XML Code: [ Select ]
<cars>
   <car>
      <make>Accord</make>
      <year>2004</year>
      <model>Honda</model>
      <mileage>46251</mileage>
      <condition>Excelent</condition>
      <title>Clear</title>
   </car>
   <car>
      <make>Cobalt 2 Door Coupe</make>
      <year>2006</year>
      <model>Chevrolet</model>
      <mileage>40265</mileage>
      <condition>Good</condition>
      <title>Salvage</title>
   </car>
   <car>
      <make>Camry</make>
      <year>2000</year>
      <model>Toyota</model>
      <mileage>115015</mileage>
      <condition>Fair</condition>
      <title>Clear</title>
   </car>
   <car>
      <make>Civic Hybrid</make>
      <year>2005</year>
      <model>Honda</model>
      <mileage>59823</mileage>
      <condition>Excelent</condition>
      <title>Clear</title>
   </car>
   <car>
      <make>Tundra 4x4</make>
      <year>2008</year>
      <model>Toyota</model>
      <mileage>12000</mileage>
      <condition>Excelent</condition>
      <title>Clear</title>
   </car>
   <car>
      <make>Carvette</make>
      <year>2002</year>
      <model>Chevrolet</model>
      <mileage>60823</mileage>
      <condition>Good</condition>
      <title>Salvage</title>
   </car>
</cars>
  1. <cars>
  2.    <car>
  3.       <make>Accord</make>
  4.       <year>2004</year>
  5.       <model>Honda</model>
  6.       <mileage>46251</mileage>
  7.       <condition>Excelent</condition>
  8.       <title>Clear</title>
  9.    </car>
  10.    <car>
  11.       <make>Cobalt 2 Door Coupe</make>
  12.       <year>2006</year>
  13.       <model>Chevrolet</model>
  14.       <mileage>40265</mileage>
  15.       <condition>Good</condition>
  16.       <title>Salvage</title>
  17.    </car>
  18.    <car>
  19.       <make>Camry</make>
  20.       <year>2000</year>
  21.       <model>Toyota</model>
  22.       <mileage>115015</mileage>
  23.       <condition>Fair</condition>
  24.       <title>Clear</title>
  25.    </car>
  26.    <car>
  27.       <make>Civic Hybrid</make>
  28.       <year>2005</year>
  29.       <model>Honda</model>
  30.       <mileage>59823</mileage>
  31.       <condition>Excelent</condition>
  32.       <title>Clear</title>
  33.    </car>
  34.    <car>
  35.       <make>Tundra 4x4</make>
  36.       <year>2008</year>
  37.       <model>Toyota</model>
  38.       <mileage>12000</mileage>
  39.       <condition>Excelent</condition>
  40.       <title>Clear</title>
  41.    </car>
  42.    <car>
  43.       <make>Carvette</make>
  44.       <year>2002</year>
  45.       <model>Chevrolet</model>
  46.       <mileage>60823</mileage>
  47.       <condition>Good</condition>
  48.       <title>Salvage</title>
  49.    </car>
  50. </cars>

index.php
Code: [ Select ]
<html>
<head>
<title>Cars</title>
</head>
<body>
<p><a href="?pg=add">Add</a> | <a href="?pg=remove">Remove</a> | <a href="?pg=search">Search</a> | <a href="index.php">View All</a></p>
<?php

$contents = file_get_contents('test.xml');
$xml = new SimpleXMLElement($contents);

$pg = $_GET['pg'];

switch($pg)
{

    case 'add':
        echo "add";
        break;
    case 'remove':
        echo "remove";
        break;
    case 'search':
?>
<h1>Search Database</h1>
<form action="index.php?pg=search" method="post">
<input type="text" name="search" value="<?=$_POST['search'] ?>" size="50" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
        if(isset($_POST['submit']))
        {
            echo "<ol>";
            foreach($xml as $xml2)
            {
                if($_POST['search'] == $xml2->model)
                {
                    echo "<li>{$xml2->year} {$xml2->model} {$xml2->make}</li>";
                }
            }
            echo "</ol>";
        }
        break;
    default:
        echo "<ol>";
        foreach($xml as $xml2)
        {
            echo "<li>{$xml2->year} {$xml2->model} {$xml2->make}</li>";
        }
        echo "</ol>";
        break;

}

?>
</html>
  1. <html>
  2. <head>
  3. <title>Cars</title>
  4. </head>
  5. <body>
  6. <p><a href="?pg=add">Add</a> | <a href="?pg=remove">Remove</a> | <a href="?pg=search">Search</a> | <a href="index.php">View All</a></p>
  7. <?php
  8. $contents = file_get_contents('test.xml');
  9. $xml = new SimpleXMLElement($contents);
  10. $pg = $_GET['pg'];
  11. switch($pg)
  12. {
  13.     case 'add':
  14.         echo "add";
  15.         break;
  16.     case 'remove':
  17.         echo "remove";
  18.         break;
  19.     case 'search':
  20. ?>
  21. <h1>Search Database</h1>
  22. <form action="index.php?pg=search" method="post">
  23. <input type="text" name="search" value="<?=$_POST['search'] ?>" size="50" />
  24. <input type="submit" name="submit" value="submit" />
  25. </form>
  26. <?php
  27.         if(isset($_POST['submit']))
  28.         {
  29.             echo "<ol>";
  30.             foreach($xml as $xml2)
  31.             {
  32.                 if($_POST['search'] == $xml2->model)
  33.                 {
  34.                     echo "<li>{$xml2->year} {$xml2->model} {$xml2->make}</li>";
  35.                 }
  36.             }
  37.             echo "</ol>";
  38.         }
  39.         break;
  40.     default:
  41.         echo "<ol>";
  42.         foreach($xml as $xml2)
  43.         {
  44.             echo "<li>{$xml2->year} {$xml2->model} {$xml2->make}</li>";
  45.         }
  46.         echo "</ol>";
  47.         break;
  48. }
  49. ?>
  50. </html>

What I don't know is how to do the "Remove" function for this. How do I remove one entry that I choose in the XML? Also, what is the best way to add entries to it? I was thinking simply a w+ for fopen(); and adding things to the bottom and then adding the <cars> tag the very last in the process rather than in the XML file. Is there a better way?

Do I have to use regex for removing entries (a specific one, not the first or last one...). Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • cipher
  • Graduate
  • Graduate
  • User avatar
  • Posts: 157

Post 3+ Months Ago

I think SimpleXML is ill-equipped to handle this. Assuming that you are referring to removing a node, I'd suggest looking into Dom XML (http://us.php.net/manual/en/book.domxml.php) since it provides more methods for handling CRUD operations.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Man, for some reason that looks confusing... but I'll still give it a try though... try to understand it :lol:

Thanks for that information, if anyone actually knows how to actually do that, than that would be great.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

DOMDocument is what you're looking for. Scroll down to take a look at the available methods for the DOMDocument class, specifically the constructor and the saveXML() method.

http://us3.php.net/manual/en/class.domdocument.php
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Thanks spork. I'll try to get some sense out of that :lol:
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Here's something to start you off:

Code: [ Select ]
$xml = new DOMDocument();
$xml->loadXML($string_containing_xml);
// alternatively, you can use load() and specify a filename

// use other DOMDocument methods to modify the XML here

$xml->save();
  1. $xml = new DOMDocument();
  2. $xml->loadXML($string_containing_xml);
  3. // alternatively, you can use load() and specify a filename
  4. // use other DOMDocument methods to modify the XML here
  5. $xml->save();
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Oh... I understood that part... I just need to find out/understand how to remove a certain node or child or w/e from the xml. I hope you understand... if not, than I'll try to explain in more visual terms

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 146 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.