Can you use php in xml

PHP XML Tutorial: Create, Parse, Read with Example

XML is used to structure, store and transport data from one system to another.

It uses opening and closing tags.

Unlike HTML, XML allows users to define their own tags.

In this tutorial, you will learn-

What is DOM?

DOM is the acronym for Document Object Model.

It’s a cross platform and language neutral standard that defines how to access and manipulate data in;

DOM XML is used to access and manipulate XML documents. It views the XML document as a tree-structure.

XML Parsers

An XML parser is a program that translates the XML document into an XML Document Object Model (DOM) Object.

The XML DOM Object can then be manipulated using JavaScript, Python, and PHP etc.

The keyword CDATA which is the acronym for (Unparsed) Character Data is used to ignore special characters such as “” when parsing an XML document.

Why use XML?

  • Web services such as SOAP and REST use XML format to exchange information. Learning what XML is and how it works will get you competitive advantage as a developer since modern applications make heavy use of web services.
  • XML documents can be used to store configuration settings of an application
  • It allows you to create your own custom tags which make it more flexible.

XML Document example

Let’s suppose that you are developing an application that gets data from a web service in XML format.

Below is the sample of how the XML document looks like.

   Joe Paul CEO  Tasha Smith Finance Manager  
  • “” specifies the xml version to be used and encoding
  • “” is the root element.
  • “…” are the child elements of administration and sales respectively.

How to Read XML using PHP

Let’s now write the code that will read the employees XML document and display the results in a web browser. Index.php

Employees Listing'; $list = $xml->record; for ($i = 0; $i < count($list); $i++) < echo 'Man no: ' . $list[$i]->attributes()->man_no . '
'; echo 'Name: ' . $list[$i]->name . '
'; echo 'Position: ' . $list[$i]->position . '

'; > ?>
  • “$xml = simplexml_load_file(’employees.xml’);” uses the simplexml_load_file function to load the file name employees.xml and assign the contents to the array variable $xml.
  • “$list = $xml->record;” gets the contents of the record node.
  • “for ($i = 0; $i < count(…)…” is the for loop that reads the numeric array and outputs the results
  • “$list[$i]->attributes()->man_no;” reads the man_no attribute of the element
  • “$list[$i]->name;” reads the value of the name child element
  • “$list[$i]->position;” reads the value of the position child element

Testing our application

Assuming you saved the file index.php in phptus/xml folder, browse to the URL http://localhost/phptuts/xml/index.php

PHP XML, DOM, Parser

How to Create an XML document using PHP

We will now look at how to create an XML document using PHP.

We will use the example above in the DOM tree diagram.

The following code uses the PHP built in class DOMDocument to create an XML document.

encoding = 'utf-8'; $dom->xmlVersion = '1.0'; $dom->formatOutput = true; $xml_file_name = 'movies_list.xml'; $root = $dom->createElement('Movies'); $movie_node = $dom->createElement('movie'); $attr_movie_id = new DOMAttr('movie_id', '5467'); $movie_node->setAttributeNode($attr_movie_id); $child_node_title = $dom->createElement('Title', 'The Campaign'); $movie_node->appendChild($child_node_title); $child_node_year = $dom->createElement('Year', 2012); $movie_node->appendChild($child_node_year); $child_node_genre = $dom->createElement('Genre', 'The Campaign'); $movie_node->appendChild($child_node_genre); $child_node_ratings = $dom->createElement('Ratings', 6.2); $movie_node->appendChild($child_node_ratings); $root->appendChild($movie_node); $dom->appendChild($root); $dom->save($xml_file_name); echo "$xml_file_name has been successfully created"; ?>

Testing our application

Assuming you saved the file create_movies_list in phptuts/xml folder, browse to the URL http://localhost/phptuts/xml/create_movies_list.php

PHP XML, DOM, Parser

Click on movies_list_xml link

PHP XML, DOM, Parser

Summary

  • XML is the acronym for Extensible Markup Language
  • XML can be used for exchanging information between systems or store configuration settings of an application etc.
  • DOM is the acronym for Document Object Model. XML DOM views the XML document as a tree-structure
  • An XML Parser is a program that translates XML an XML document into a DOM tree-structure like document.
  • CDATA is used to ignore special characters when parsing XML documents.
  • PHP uses the simplexml_load_file to read XML documents and return the results as a numeric array
  • PHP DOMDocument class to create XML files.

Источник

Including PHP in XML

Above you can see that I am trying just to echo test just to get it working. anything after the nothing shows up including the echo this file is called by a flash file (i included this info in-case its important) thanks in advance

Are you generating the xml file in PHP and then displaying it in a browser or are you trying to display an xml file with the PHP tags in it as above?

It sounds like the web server doesn’t recognise your file as a PHP file and therefore PHP ain’t running. Your filename probably ends in .xml instead of .php

You could try setting your apache ( I presume ) server to process xml files as php though I think you’d be better using PHP to actually generate the xml file

4 Answers 4

I’d suggest using PHP to generate the XML as you go, the output of the call to DOMDocument can be either saved as a file or output as a string. In the exmple below it is simply returned as a string. The example tests for a GET parameter called uname — change that to whatever you want

createElement('config'); $root->appendChild( $dom->createElement('enable_music','true') ); $root->appendChild( $dom->createElement('music_path','music.mp3') ); $root->appendChild( $dom->createElement('mute_button_visible','true') ); $text=$dom->createElement('text_block'); $cdata=$dom->createCDATASection(' 



Dear '.$uname.'!
We wish you warm Christmas
and Happy New Year!
Be happy and keep smiling!


www.yourdomain.com

'); $text->appendChild( $cdata ); $root->appendChild( $text ); $dom->appendChild( $root ); echo ''; $dom=null; > ?>

Источник

Embedding PHP in XML

I am trying to execute PHP code in XML Below is the code is there better way of executing as we are using eval and far as I know it degrade the performance 80-85% as it is supposed to be used by browser.

function processing_instruction($inParser, $inTarget, $inCode) < if ($inTarget === 'php') < eval($inCode); >> 

Eval is not done by the browser, it’s done by the server — however it is very unsafe unless you absolutely trust your input.

3 Answers 3

«If eval() is the answer, you’re almost certainly asking the wrong question.»
-Rasmus Lerdorf, BDFL of PHP

Is the code you are running so varied that it can’t be decided upon as a series of files to be included on demand or a XML-RPC style function call? There is generally very little to gain by allowing arbitrary code execution, and that’s before you consider the staggering amount you stand to lose.

If there is a finite, predictable number of things these files could possibly do, I would Strongly recommend taking the time to create a semi-generic XML-RPC interface (or at least a series of files that you could specify in the XML file and then include on-the-fly, perhaps after setting some environment variables, depending on your coding style) and using that.

The number of risks you take when creating a portal to eval() are nigh innumerable.

I had considered providing some examples here, but XML-RPC ought to be a well enough known concept that my doing so is altogether unnecessary.

Источник

Can we use PHP in XML?

Manipulating XML file data using JavaScript. Actually, just the last two lines of the function are enough to load the XML file. The previous two lines are written to ensure that the JavaScript functions that we may use later to manipulate the XML file data, does not perform any function on an uninitialized object.

How append XML in PHP?

1 Answer. $file = ‘xml/config. xml‘; $xml = simplexml_load_file($file); $galleries = $xml->galleries; $gallery = $galleries->addChild(‘gallery’); $gallery->addChild(‘name’, ‘a gallery’); $gallery->addChild(‘filepath’, ‘path/to/gallery’); $gallery->addChild(‘thumb’, ‘mythumb. jpg’); $xml->asXML($file);

How display XML file in browser using PHP?

php header(“Content-type: text/xml”); $yourFile = “xmlfile. xml“; $file = file_get_contents($yourFile); echo $file; If you insist on simple xml you can write like this.

What is the difference between PHP and XML?

There is no relation between PHP and XML. XML is something that PHP can consume and produce. There is nowhere during processing that PHP consumes or produces XML unless you explicitly tell PHP to do so.

Is XML a script?

XML is a data representation and not a scripting language.

What can you do with XML files?

Some common uses of XML include:

  • Web publishing: With XML, users can create and customize interactive web pages. …
  • Web tasks: XML may be used for web searching and automating tasks. …
  • General applications: All kinds of applications can benefit from XML as it offers a streamlined method of accessing information.

How do I load XML?

Click Developer > Import. If you don’t see the Developer tab, see Show the Developer tab. In the Import XML dialog box, locate and select the XML data file (. xml) you want to import, and click Import.

Which function appends an element as a child of an existing element PHP?

The DOMNode::appendChild() function is an inbuilt function in PHP which is used to appends a child to an existing list of children or creates a new list of children.

When using addChild () to add a child object What does the child get added to?

The addChild() function appends a child element to the SimpleXML element.

What is simple XML extension?

SimpleXML is a PHP extension that allows users to easily manipulate/use XML data. … It represents an easy way of getting an element’s attributes and textual content if you know the XML document’s structure or layout.

What is PHP Ajax?

AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

From the author

Greetings! I present my blog about web programming languages. In this blog, I post my notes on the topic of web development languages. I am not a professional programmer, and therefore I do not always have the correct idea of what I am writing. But in most cases, I try to follow the principles of material selection that I have developed over many years of practice.

ATTENTION TO RIGHT HOLDERS! All materials are posted on the site strictly for informational and educational purposes! If you believe that the posting of any material infringes your copyright, be sure to contact us through the contact form and your material will be removed!

Источник

Читайте также:  Detect country by ip php
Оцените статью