How to Edit an XML File in PHP

How to Edit an XML File in PHP thumbnail
Edit data inside an XML document using the DOMDocument class in PHP.

Use PHP (Hypertext Preprocessor) to manage, edit and control other files of different formats on your server. Extensible Markup Language (XML) is a programming language used to transport and store data. PHP was created to use with the SQL programming language, which also helps with the storage of data. This makes PHP optimum to work with the XML programming language. PHP has a predefined class called DOMDocument that is used to work with XML documents. Use the DOMDocument class to edit XML files on your server.

Instructions

    • 1

      Open a new document in a text-editing program like Notepad or a web-authoring application such as Dreamweaver.

    • 2

      Use the "DOMDocument" class in PHP to load the XML document that you want to edit. You do this by creating a new class instance in the PHP coding. It looks something like this:

      "<?php

      $xml_Document= new DOMDocument('1.0', 'utf-8');

      $xml_Document->formatOutput = true;

      $xml_Document->preserveWhiteSpace = false;

      $xml_Document->load('example.xml');"

      Change the "example.xml" to the name of the XML document that you want to edit. Remove the quotation marks from the first and last line of the code.

    • 3

      Type in the code to load the "table" from the XML document. Load the table as an element by using the table name. For instance, if you have a "Contacts" table with column headings of "Name," "Address," "City," "State," "Zip Code" and "Telephone Number," you load "Contacts" before loading the individual columns. Load the table element with the PHP code to edit.

      "$element = $xml->getElementsByTagName('contact')->contact(0);"

      Change "contact" to the name of your table.

    • 4

      Load the child elements with the PHP code. These child elements are the names of the individual columns inside of the table.

      "$name = $element->getElementsByTagName('name')->contact(0);

      $address = $element->getElementsByTagName('address')->contact(0);

      $city = $element->getElementsByTagName('city')->contact(0);

      $state = $element->getElementsByTagName('state')->contact(0);

      $zip = $element->getElementsByTagName('zip code')->contact(0);

      $phone = $element->getElementsByTagName('telephone')->contact(0);"

      The coding represents the columns in the "Contacts" table example used above. Change the values to represent the columns in your XML document.

    • 5

      Use PHP code to edit the values in the rows of the table specified above. Specify the row that you want to change first by adding the value to the variable. Then decide which column of information that corresponds with that row you want to change. For instance, say you have a contact with the name "Jon Doe" in your "Contacts" table who has changed his phone number. To change the phone number in the XML document, pull two pieces of information that matche the "Jon Doe" record so that the code knows what row you are referring to and then change the information. The coding will look something like this:

      "$name->nodeValue = 'Jon Doe';

      $state->nodeValue = 'NY';"

      Now that you have specified what row to change, set a new variable to change the information you need to change.

      "$newPhone = $xml->createElement('telephone' 213-555-5555);

      $element->replaceChild($name, $name);

      $element->replaceChild($state,$state);

      $element->replaceChild($newPhone, $phone);

      ?>"

      As you can see, you replace the "Name" and "State" information for "Jon Doe" to the old data that you loaded from the XML document. You didn't change that information at all. But for the phone number, you changed that to the new phone number specified earlier. Use this as a road map for editing the rows inside of your XML document, using the right column names and the information that you want to change.

    • 6

      Save the PHP document and load it to your web server in the same folder as the XML document you want to edit. Run the PHP document by typing in its web address in an Internet browser and hitting "Enter" on your keyboard.

Related Searches:

References

  • Photo Credit record of data image by Witold Krasowski from Fotolia.com

Comments

You May Also Like

  • How to Write Into XML File Using PHP

    XML is a Web design standard that uses HTML to order data. For some programmers, the ability to print out XML files...

  • How to Edit XML Flash Templates

    XML stands for Extensible Markup Language ans is designed to hold and transport data. With a few lines of ActionScript, you can...

  • How to Read an XML File With PHP

    Extensible Markup Language (XML) allows you to organize information in a way that describes the information and is easy for computer programs...

  • How to Edit XML

    The XML file format is a decoded interface language that is commonly used for Microsoft Internet Explorer, programs, and file component formatting....

  • How to Convert an XML File to a PHP File

    XML (eXtensible Markup Language) is a convenient way of writing human-readable data sets which can also be shared between computer systems that...

  • How to Edit PHP Files

    PHP is a server side scripting language used in creating dynamic web pages. It is possible to use third-party PHP applications on...

  • How to Call a PHP File Name From XML Flash Code

    When you write a program in the Adobe Flash language, you can create XML files that hold file structure information in the...

  • How to Write Comments to an XML File in PHP

    When creating XML files to store data, you may wish to include comments on the data, much as you would add comments...

  • How to Convert Old XML File to Word 2007

    If you have upgraded to Word 2007, but still have old XML files stored on your system that you need to be...

  • How to Build an XML Website

    Define names for all tags. Tags can be any non-reserved tag, allowing you to be descriptive. For example, if storing the name...

  • How to Write XML Files in PHP

    Create your PHP script by opening a new file in your text editor or Integrated Development Environment and saving it with ".php"...

  • How to Update an XML File in Java

    Create a file named "test.xml" in a text editor and save it to the C: drive. Paste the following sample XML database...

  • How to Get Data in an XML File Using a Web Service

    An XML file is a type of computer file that contains information stored in a very specific Internet programming language. XML stands...

  • The PHP File Structure

    Although they appear as pictures and text on your screen, Internet webpages are created using combinations of letters, numbers and systems called...

  • How to Write a Simple PHP Script

    PHP is a popular scripting language you can use for building interactive websites or manipulating server files. PHP provides many functions that...

  • How to Use CDATA in XML

    Sometimes within the XML syntax, developers will place a string of input between character data, or CDATA brackets. The information that is...

  • How to Create a PHP Survey

    Open your text editor and type the following at the top of the document: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD...

  • How to Change XML Encoding

    Extensible Markup Language (XML) has become the most popular language for data representation, especially for electronic documents available on the Internet. Even...

Related Ads

Featured