PHP XML file additions and deletions to change

Source: Internet
Author: User

source file
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<endTime>2016-08-01</endTime>
<content> File Contents </content>
</root>

$file = ' callboard.xml ';


Query XML-----------------------

$doc =new DOMDocument ();//Instantiate a pair of images
$doc->load ($file);//Load file
$data =array ();
$time = $doc->getelementsbytagname ("EndTime");//Gets the element is the title
$dateTime = $time->item (0)->nodevalue;//Gets the value for the specified element
$content = $doc->getelementsbytagname ("content");//Gets the element is the title
$dataContent = $content->item (0)->nodevalue;//Gets the value for the specified element

Update and create-------------------

If (file_exists ($file))//Determine if file exists
     {
       //update content if it exists
             $doc =new DOMDocument ();//Instantiate a pair like
              $doc->load ($file);//Load file
              $resultDate = $doc->getelementsbytagname ("EndTime");//Get element is Datenumber
              $resultDate->item (0)->nodevalue=$_post[' time '];//assigning a value to a specified element
             $resultBall 1= $doc->getelementsbytagname ("content");//Get element is Ball1
             $resultBall 1->item (0)->nodevalue=$_post[' Content '];//assigns a value to the specified element
             $doc->save ($file);// Use this method when it is useful to modify
        }
     Else
     {

Create a file if it does not exist
$doc =new DOMDocument (' 1.0 ', ' utf-8 ');//Declaration is XML
$doc->formatoutput=true;//Format output
$root = $doc->createelement (' root ');//Create root element
$endTime = $doc->createelement ("EndTime", $_post[' time ');//Create element Datenumber and assign value $arr[' resultdate '] content
$content = $doc->createelement ("content", $_post[' content ');//Create element Ball1 and assign value $arr[' resultBall1 '] content
$root->appendchild ($endTime);//$content element contains $datenumber elements
$root->appendchild ($content);//$content element contains $ball1 elements
$doc->appendchild ($root);//end establishment of root meta-cable
$doc->save ($file);//Generate Xml.xml file
}

PHP XML file additions and deletions to change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.