Cara menggunakan php create xml

In this article, we show how to create an XML document with PHP.

This means that we use PHP code to write the contents of an XML document.

This can be done in actually a few ways.

The 2 most popular ways to write XML code is through the SimpleXML extension or the DOM extension.

Both are effective ways of creating XML content via PHP.

The DOM can be described as more powerful and complex, while SimpleXML is simpler.

Below we show how to create an XML document using PHP through the SimpleXML extension.

So the first thing we do in this code is to create the $simplexml variable. This creates a new XML element, where we declare the document to be an XML document of version 1.0. We then create the root elment, books.

We then create the first element within the root element, book. We create a variable named $book1 and add the element, book. Within this book element, we add the booktitle element with the contents in between the booktitle tags being "The Wandering Oz". We then add the publicationdate element with the contents in between the publicationdate tags being 2007.

We then go onto the next book element. We again add the child element book. To this book element we add the booktitle element with the contents of "The Roaming Fox". We then add the publicationdate element with the contents being 2009.

We then create one last book element. We add a booktitle element with the contnets of "The Dominant Lion" and a publicationdate element with the contents being 2012.

We then echo out all of these XML instructions through the statement, $simplexml->asXML[].

This displays all of the tags we entered and actually renders it as XML.

Being that the XML declaration has to go to the top of the page of a web document, I can't show how it would render by placing the PHP below. Instead, I have to create a new PHP file with solely this code in it. That way, the XML declaration is at the top of the page.

Doing so yields the following PHP page: books.php. ?>

Creating a Separate XML Document

The following code above is great and generates XML. However, the XML data is generated on a PHP page. This is probably not what you want. However, you more than likely want XML generated on a complete and separate XML page. This way, it's just as if you created an XML document [with .xml extension] and wrote it complete with XML tags.

To do this, the following code above, just needs a slight modification.

We add in the file_put_contents[] function into the code and into this function we create a separate XML file called books.xml.

So you can see now we have most of the code we previously had, but instead of echoing the contents of the XML tags we wrote onto the current PHP page, we use the file_put_contents[] function to create an XML file named books.xml. We then write the contents of the XML tags we wrote into this books.xml file.

So we now have a separate XML document. The following link shows this XML document: books.xml.

You can see how it's a perfect-looking XML document that has been generated through PHP.

This is the power of PHP and XML. PHP has built-in code that allows you to work very well with XML.

The advantage of using this code is that you generate dynamic XML on the fly with PHP. You don't simply have to write XML code. XML code can be generated dynamically through a dynamic language such as PHP.

Related Resources

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we are going to display data present in an XML file on a web page using PHP through the XAMPP server. PHP is a server-side scripting language that is mainly for processing web data. The XML stands for an extensible markup language.

    Requirements:

    • XAMPP server

    Syntax:

       
       .....
       
    

    Approach: We are going to use mainly two functions in our PHP code. The simplexml_load_file[] function is used to convert an XML document to an object.

    • simplexml_load_file
      simplexml_load_file[name of XML file]
      
    • children[]: The children[] function finds the children of a specified node.
      $xml_data->children[]
      

    Steps to execute:

    • Step 1: Start XAMPP server.
    • Open notepad and type the following codes in xml_data.xml and code.php formats The xml_data.xml: Consider student XML data as an example.

      xml_data.xml

         

            java

            G.Sravan Kumar

            22

            98

            guntur

         

         

            Python

            B. Naga sudheer

            28

            96

             guntur

         

         

            sql

            Radha

            25

            78

             guntur

         

    • Step 3:The following is the code for code.php file.

      code.php

    • Step 4: Save these two files in xampp/htdocs/geek folder. The developer can use any other folder instead of geek folder.

    Output: Open your browser and type localhost/geek/code.php to see the output.

    XML data 


    Can you use PHP in XML?

    If you run the XML file as a PHP script, then yes.

    HOW include XML file in PHP?

    Example. $xml=simplexml_load_file["note. xml"]; print_r[$xml];

    Is XML and PHP same?

    XML documents use something called a schema which describes the structure of the XML itself, and so you can validate an XML document against a schema to make sure that it is well-formed. There is no relation between PHP and XML. XML is something that PHP can consume and produce.

    Why XML is used in PHP?

    XML is used to structure, store and transport data from one system to another. XML is similar to HTML. It uses opening and closing tags. Unlike HTML, XML allows users to define their own tags.

    Bài mới nhất

    Chủ Đề