Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 31

Thread: Read and write XML with PHP

  1. #21
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default pulling a single bit of info from an xml

    Hi everyone...I'm extremely new to both xml and php and I'm trying to get my bearings here.

    I’ve looked at so much information but can’t find a simple script that will let me pull a simple object out of a xml if I already know what the id for that object is. Can someone get me started here? Say I want to pull the name for id 2 from the xml?

    If you have a simple xml called friends.xml

    <people>
    <id>1</id>
    <name>Joe</name>
    </people>
    <people>
    <id>2</id>
    <name>Mike</name>
    </people>

    With simplexml I can get to the file by doing

    $myfriends = simplexml_load_file(friends.xml);

    But I can't find any info on how to pull a single piece of info from an xml.

  2. #22
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    XML functions for PHP typically load the information into an array. From there you would just search the array at the right level. For example, it may be as simple as $myfriends[1], if that is how your XML is setup, but most likely instead it will mean looping through all of the array until you find one where ($thisfriend['id'] == 1).

    A database will ALWAYS be better if you intend to search/cross-reference data. XML is possible, but for exactly the reasons you're seeing difficult and at some level of complexity may become impossible.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #23
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by codeexploiter View Post

    Writing XML using PHP

    PHP Code:
    <?php
      $employees 
    = array();
      
    $employees [] = array(
      
    'name' => 'Albert',
      
    'age' => '34',
      
    'salary' => "$10000"
      
    );
      
    $employees [] = array(
      
    'name' => 'Claud',
      
    'age' => '20',
      
    'salary' => "$2000"
      
    );
      
      
    $doc = new DOMDocument();
      
    $doc->formatOutput true;
      
      
    $r $doc->createElement"employees" );
      
    $doc->appendChild$r );
      
      foreach( 
    $employees as $employee )
      {
      
    $b $doc->createElement"employee" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild(
      
    $doc->createTextNode$employee['name'] )
      );
      
    $b->appendChild$name );
      
      
    $age $doc->createElement"age" );
      
    $age->appendChild(
      
    $doc->createTextNode$employee['age'] )
      );
      
    $b->appendChild$age );
      
      
    $salary $doc->createElement"salary" );
      
    $salary->appendChild(
      
    $doc->createTextNode$employee['salary'] )
      );
      
    $b->appendChild$salary );
      
      
    $r->appendChild$b );
      }
      
      echo 
    $doc->saveXML();
      
    $doc->save("write.xml")
      
    ?>
    This code will output the XML file in console as well as it will create a XML file in the name of write.xml in the same directory of the PHP script.

    As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.
    What if you only want to change a single item...such as Claud's age from 20 to 21 because he lied about his age? How would that work?

    btw: you all are giving me some really good information here.

  4. #24
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You would find a way to modify that age value. Find the right reference (such as $object->age or $object['age'], or however you are accessing it), then just change it using $var = $value; syntax.
    THEN you need to write that entire XML structure back to the file. You can't just "change" it.
    So if you are using functions that have something like xml_write_file($data), then you can do that, or you can construct it yourself by writing out the structure (easier using the first method). Then just store that XML into a file using fwrite() or maybe that's already built into your functions.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #25
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Having trouble writing back

    Still trying to figure this out. So I created a counter and am trying to read then add 1 to counter before replacing orginal number. I can read fine but the writing I've got all messed up.

    Can someone tell me what I'm doing wrong here?

    XML File named counter.xml
    <?xml version="1.0" encoding="utf-8"?>
    <counters>
    <mycount>
    <numcount>1</numcount>
    </mycount>
    </counters>

    I get the count
    <?php
    //get count
    $doc = new DOMDocument();
    $doc->load('counter.xml');

    $mycount = $doc->getElementsByTagName( "mycount" );
    $numcount = $mycount->item(0)->nodeValue;

    echo"<input type='text' name='count' id='count' value=$numcount>"
    ?>

    Add 1 and write back...so I don't need an array with just one number but an array can have one number. I wanted to make it so it would work on things that have multiple elements.

    <?php
    //function called from web page
    function newquote()
    {
    //change counter
    $count = document.insertsidebar.count.value + 1;
    //writing to counter xml
    $mycount = array();
    $mycount [] = array('mycount' => $count);
    $doc = new DOMDocument();
    $doc->formatOutput = true;
    $r = $doc->createElement(counters);
    $doc->appendChild($r);
    foreach ($counters as $mycount)
    {
    $b = $doc->createElement( "counter" );
    $count = $doc->createElement( "count" );
    $b->appendChild( "counter" );
    $r->appendChild( $b );
    }

    echo $doc->saveXML();
    $doc->save("counter.xml");
    }
    ?>

  6. #26
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    Reading an XML File using PHP

    HTML Code:
    XML Code: file name - employees.xml
    
    <?xml version="1.0" encoding="iso-8859-1"?>
    <employees>
      <employee>
      <name>Mark</name>
      <age>27</age>
      <salary>$5000</salary>
      </employee>
      <employee>
      <name>Jack</name>
      <age>25</age>
      <salary>$4000</salary>
      </employee>
      </employees>
    PHP Code:
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'employees.xml' );
      
    $employees $doc->getElementsByTagName"employee" );
    foreach( 
    $employees as $employee )
    {
      
    $names $employee->getElementsByTagName"name" );
      
    $name $names->item(0)->nodeValue;
      
      
    $ages$employee->getElementsByTagName"age" );
      
    $age$ages->item(0)->nodeValue;
      
      
    $salaries $employee->getElementsByTagName"salary" );
      
    $salary $salaries->item(0)->nodeValue;
      
      echo 
    "<b>$name - $age - $salary\n</b><br>";
      }
    ?>
    With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.

    Writing XML using PHP

    PHP Code:
    <?php
      $employees 
    = array();
      
    $employees [] = array(
      
    'name' => 'Albert',
      
    'age' => '34',
      
    'salary' => "$10000"
      
    );
      
    $employees [] = array(
      
    'name' => 'Claud',
      
    'age' => '20',
      
    'salary' => "$2000"
      
    );
      
      
    $doc = new DOMDocument();
      
    $doc->formatOutput true;
      
      
    $r $doc->createElement"employees" );
      
    $doc->appendChild$r );
      
      foreach( 
    $employees as $employee )
      {
      
    $b $doc->createElement"employee" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild(
      
    $doc->createTextNode$employee['name'] )
      );
      
    $b->appendChild$name );
      
      
    $age $doc->createElement"age" );
      
    $age->appendChild(
      
    $doc->createTextNode$employee['age'] )
      );
      
    $b->appendChild$age );
      
      
    $salary $doc->createElement"salary" );
      
    $salary->appendChild(
      
    $doc->createTextNode$employee['salary'] )
      );
      
    $b->appendChild$salary );
      
      
    $r->appendChild$b );
      }
      
      echo 
    $doc->saveXML();
      
    $doc->save("write.xml")
      
    ?>
    This code will output the XML file in console as well as it will create a XML file in the name of write.xml in the same directory of the PHP script.

    As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.
    I see how to read and how to write.
    How would you add a record after reading and before writing?

    or is that what this part is?
    $employees [] = array(
    'name' => 'Albert',
    'age' => '34',
    'salary' => "$10000"
    );
    $employees [] = array(
    'name' => 'Claud',
    'age' => '20',
    'salary' => "$2000"
    );

  7. #27
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    how would you either make the form enter the post as first one (first child) into xml or get the php to load from the bottom up. in example: i want to load date and time items in order from most recent.

  8. #28
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Quote Originally Posted by codeexploiter View Post
    Reading an XML File using PHP

    HTML Code:
    XML Code: file name - employees.xml
    
    <?xml version="1.0" encoding="iso-8859-1"?>
    <employees>
      <employee>
      <name>Mark</name>
      <age>27</age>
      <salary>$5000</salary>
      </employee>
      <employee>
      <name>Jack</name>
      <age>25</age>
      <salary>$4000</salary>
      </employee>
      </employees>
    PHP Code:
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'employees.xml' );
      
    $employees $doc->getElementsByTagName"employee" );
    foreach( 
    $employees as $employee )
    {
      
    $names $employee->getElementsByTagName"name" );
      
    $name $names->item(0)->nodeValue;
      
      
    $ages$employee->getElementsByTagName"age" );
      
    $age$ages->item(0)->nodeValue;
      
      
    $salaries $employee->getElementsByTagName"salary" );
      
    $salary $salaries->item(0)->nodeValue;
      
      echo 
    "<b>$name - $age - $salary\n</b><br>";
      }
    ?>
    With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.

    Writing XML using PHP

    PHP Code:
    <?php
      $employees 
    = array();
      
    $employees [] = array(
      
    'name' => 'Albert',
      
    'age' => '34',
      
    'salary' => "$10000"
      
    );
      
    $employees [] = array(
      
    'name' => 'Claud',
      
    'age' => '20',
      
    'salary' => "$2000"
      
    );
      
      
    $doc = new DOMDocument();
      
    $doc->formatOutput true;
      
      
    $r $doc->createElement"employees" );
      
    $doc->appendChild$r );
      
      foreach( 
    $employees as $employee )
      {
      
    $b $doc->createElement"employee" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild(
      
    $doc->createTextNode$employee['name'] )
      );
      
    $b->appendChild$name );
      
      
    $age $doc->createElement"age" );
      
    $age->appendChild(
      
    $doc->createTextNode$employee['age'] )
      );
      
    $b->appendChild$age );
      
      
    $salary $doc->createElement"salary" );
      
    $salary->appendChild(
      
    $doc->createTextNode$employee['salary'] )
      );
      
    $b->appendChild$salary );
      
      
    $r->appendChild$b );
      }
      
      echo 
    $doc->saveXML();
      
    $doc->save("write.xml")
      
    ?>
    This code will output the XML file in console as well as it will create a XML file in the name of write.xml in the same directory of the PHP script.

    As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.

    Hey guys, i ve used all this php code successfully (THX!)! how can i write/create a second XML file in the same code (with some of the same variables and some others)? thanks in advance!

  9. #29
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Need Help in Making XML Dynamic

    Below is the code in my slideshow.php which is behaving as XML properly But What want is My music and images should be dynamically fetched from database.How can i do that.
    PHP Code:
    <?php   
    file_put_contents
    ('slideshow.xml''<?xml version="1.0" encoding="iso-8859-1"?> 
        <slideshow  displayTime="5"  
                    transitionSpeed=".7" 
                    transitionType="Fade" 
                    motionType="None" 
                    motionEasing="easeInOut" 
                    randomize="false" 
                    slideshowWidth="400" 
                    slideshowHeight="220" 
                    slideshowX="center" 
                    slideshowY="0" 
                    bgColor="FFFFFF" 
                    bgOpacity="100" 
                    useHtml="true" 
                    showHideCaption="false" 
                    captionBg="000000" 
                    captionBgOpacity="0" 
                    captionTextSize="11" 
                    captionTextColor="FFFFFF" 
                    captionBold="false" 
                    captionPadding="7" 
                    showNav="false" 
                    autoHideNav="false" 
                    navHiddenOpacity="40" 
                    navX="335" 
                    navY="193" 
                    btnColor="FFFFFF" 
                    btnHoverColor="FFCC00" 
                    btnShadowOpacity="85" 
                    btnGradientOpacity="20" 
                    btnScale="120" 
                    btnSpace="7" 
                    navBgColor="333333" 
                    navBgAlpha="0" 
                    navCornerRadius="0" 
                    navBorderWidth="1" 
                    navBorderColor="FFFFFF" 
                    navBorderAlpha="0" 
                    navPadding="8" 
                    tooltipSize="8" 
                    tooltipColor="000000" 
                    tooltipBold="true" 
                    tooltipFill="FFFFFF" 
                    tooltipStrokeColor="000000" 
                    tooltipFillAlpha="80" 
                    tooltipStroke="0" 
                    tooltipStrokeAlpha="0" 
                    tooltipCornerRadius="8" 
                    loaderWidth="200" 
                    loaderHeight="1" 
                    loaderColor="FF0000" 
                    loaderOpacity="100"  
                     
                    attachCaptionToImage="true" 
                    cropImages="false" 
                    slideshowMargin="0" 
                    showMusicButton="false" 
                    music="[COLOR="Red"]upload/1.mp3[/COLOR]" 
                    musicVolume="50" 
                    musicMuted="false" 
                    musicLoop="true" 
                    watermark="" 
                    watermarkX="625" 
                    watermarkY="30" 
                    watermarkOpacity="100" 
                    watermarkLink="" 
                    watermarkLinkTarget="_blank" 
                    captionsY="bottom" 
                    > 
            <image img="[COLOR="red"]upload/2.jpg[/COLOR]" customtitle="Camomile" /> 
            <image img="[COLOR="Red"]upload/3.jpg[/COLOR]" customtitle="Bells" /> 
            <image img="[COLOR="Red"]upload/4.jpg[/COLOR]" customtitle="Market" /> 
            <image img="[COLOR="Red"]upload/5.jpg[/COLOR]" customtitle="Market" /> 

        </slideshow>'
    LOCK_EX); 
    ?>
    Last edited by jscheuer1; 02-21-2012 at 10:26 AM. Reason: Format

  10. #30
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default XML to PHP

    hey guys,


    Im having trouble with this . I need to convert my xml file to php . Can someone help me asap .

    thanks

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
  •