PHP code to read XML values (recommended) _php tutorial

Source: Internet
Author: User
The simplest case for reading XML in PHP:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:


1
2
3


The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
?>

Results of cy.php Operation:
1
2
3

=============

Reading XML in PHP is a little more complicated:

Cy.xml as follows:
Copy CodeThe code is as follows:



1
2
3



The cy.php is as follows (and in the first case there is no change):
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
?>

The cy.php results are the same as for the first time:
1
2
3

PHP read the third case of XML:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:



1
2
3


4
5
6



The php file (cy.php) is as follows (and the first time remains unchanged):
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
?>

Output from cy.php:
1
2
3
4
5
6

===========

PHP reads XML fourth case, keep cy.xml unchanged, change cy.php:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:



1
2
3


4
5
6



The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
$main = $xml->getelementsbytagname (' main ');
foreach ($main as $main)
{
$list = $main->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
}
?>

cy.php Output Results:
1
2
3
4
5
6

Why are the cy.php different two times, but the output is the same? Let's take a look at the next example.

==============

PHP read the fifth case of XML, change cy.xml, so that cy.php keep the fourth case:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:




1
2
3


4
5
6


7
8
9




PHP files (cy.php) are the same as in the fourth case:
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
$main = $xml->getelementsbytagname (' main ');
foreach ($main as $main)
{
$list = $main->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
}
?>

The cy.php output results are:
1
2
3
4
5
6

Why

7
8
9

Did the 7,8,9 not be read?
Because our cy.php only read The contents of the tag. The content in the tag is not read.
The "tags" we refer to here are called "nodes" in XML;
The related concepts of "node" are explained later.

PHP reads XML case six, and then foreach once, we read out the 7,8,9!:

The XML file (CY.XM) is as follows:
Copy CodeThe code is as follows:



1
2
3


4
5
6


7
8
9



The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
$main = $xml->getelementsbytagname (' main ');
foreach ($main as $main)
{
$list = $main->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
}
$m = $xml->getelementsbytagname (' m ');
foreach ($m as $m)
{
$list = $m->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
}
?>

cy.php Output Results:
1
2
3
4
5
6
7
8
9

===============

PHP reads XML case seven, Cy.xml becomes more complex:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:



A
1
2
3


B
4
5
6


C
7
8
9



So, how do we read only Within <title></title>What's the value in?

The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
$main = $xml->getelementsbytagname (' main ');
foreach ($main as $main)
{
$list = $main->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
$title = $main->getelementsbytagname ("title");
foreach ($title as $title)
{
$value = $title->firstchild->nodevalue;
echo $value. "
”;
}
}

$m = $xml->getelementsbytagname (' m ');
foreach ($m as $m)
{
$list = $m->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. "
”;
}
}
?>

cy.php Output Results:
1
2
3
A
4
5
6

7
8
9

Think about how to read out In <title></title>The value?

PHP reads XML and reinforces it with an example:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:



This is Text one
This is Text
This is Text three


This is the Text four
This is Text Five
This is Text Six



The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = new DOMDocument (); Build a DOMDocument
$xml->load (' cy.xml '); PHP specifies where the XML file needs to be read
$LevelOne = $xml->getelementsbytagname (' Levelone ');//Get the node by name, return the collection of all nodes, but there is no point in reading Levelone here ....
$LevelOne = $xml->getelementsbytagname (' Levelone ')->item (0);//Returns the contents of the first Levelone node
$LevelTwo = $LevelOne->getelementsbytagname (' leveltwo '); Get node by name, return all Leveltwo
The foreach ($LevelTwo as $Content)//loop reads out all Leveltwo, and in the loop, the Leveltwo is expressed in Content
{
$LevelThree = $Content->getelementsbytagname (' levelthree ');//Return all Levelthree
foreach ($LevelThree as $Concert)
{
$name = $Concert->nodename;//node name
$value = $Concert->nodevalue;//node value
$id = $Concert->getattribute (' id ');//"id" property value
echo $name. "
”;
echo $value. "
”;
echo $id. "
”;
}
}
?>

If the node is obtained in a way such as $levelone = $xml->getelementsbytagname (' Levelone '), then reading the contents inside requires a foreach loop because $levelone = $xml getElementsByTagName (' Levelone ') returns a collection, not a specific node--only one node called Levelone ....
If the node is obtained using $levelone = $xml->getelementsbytagname (' Levelone ')->item (0), then the contents can be read directly $levelone->xxxxxx , because this returns a specific node.

Now provides an easy way to read XML in PHP:

The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:




Czbin XML section
Related Articles of XML


Czbin PHP Section
Related articles in PHP


Czbin Ajax Section
Articles related to Ajax




The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = simplexml_load_file (' Sxml.xml ');
$part = $xml->site->part;
foreach ($part as $content)
{
echo $content [' ID ']. "
”;
echo $content->title. "
”;
echo $content->title[' id ']. "
”;
echo $content->describe. "
”;
}
?>

cy.php Output Results:
1
Czbin XML section
A
Related Articles of XML
2
Czbin PHP Section

Related articles in PHP
3
Czbin Ajax Section
C
Articles related to Ajax

What do you think? It's very simple indeed!

http://www.bkjia.com/PHPjc/322729.html www.bkjia.com true http://www.bkjia.com/PHPjc/322729.html techarticle The simplest case for reading XML in PHP: The XML file (Cy.xml) is as follows: Copy the code as follows:? xml version= "1.0″encoding=" Gb2312″? xml list1/list list2/list list3/ List/xml php file ...

  • 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.