PHP code to read XML values (recommended) _php tips

Source: Internet
Author: User
Tags php code
The simplest scenario for reading XML in PHP:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<list>1</list>
<list>2</list>
<list>3</list>
</xml>

The php file (cy.php) is as follows:
Copy Code code as follows:

<?php
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
?>

The results of the cy.php operation:
1
2
3

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

Reading XML in PHP is slightly more complex:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<main>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
</xml>

Cy.php is as follows (and the first situation does not change):
Copy Code code as follows:

<?php
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
?>

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

PHP reads XML in the third case:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<main>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
<main>
<list>4</list>
<list>5</list>
<list>6</list>
</main>
</xml>

The php file (cy.php) is as follows (and for the first time remains unchanged):
Copy Code code as follows:

<?php
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
?>

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

===========

PHP read the fourth case of XML, keep cy.xml unchanged, change cy.php:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<main>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
<main>
<list>4</list>
<list>5</list>
<list>6</list>
</main>
</xml>

The php file (cy.php) is as follows:
Copy Code code as follows:

<?php
$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. " <br/> ";
}
}
?>

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

Why are the two cy.php different, 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 maintain the fourth case:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>

<main>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
<main>
<list>4</list>
<list>5</list>
<list>6</list>
</main>
<m>
<list>7</list>
<list>8</list>
<list>9</list>
</m>

</xml>

The php file (cy.php) is the same as the fourth:
Copy Code code as follows:

<?php
$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. " <br/> ";
}
}
?>

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

Why
<m>
<list>7</list>
<list>8</list>
<list>9</list>
</m>
7,8,9 is not read out?
Because our cy.php only reads the content in the <main></main> tag .<m></m> the content in the tag is not read.
The "tags" we refer to here are called "nodes" in XML;
About the concept of "node", we will explain later.

PHP reads XML Six, again foreach, we read 7,8,9!:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<main>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
<main>
<list>4</list>
<list>5</list>
<list>6</list>
</main>
<m>
<list>7</list>
<list>8</list>
<list>9</list>
</m>
</xml>

The php file (cy.php) is as follows:
Copy Code code as follows:

<?php
$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. " <br/> ";
}
}
$m = $xml->getelementsbytagname (' m ');
foreach ($m as $m)
{
$list = $m->getelementsbytagname ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
}
?>

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

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

PHP reads XML Seven, Cy.xml becomes more complex:

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

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<main>
<title>a</title>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
<main>
<title>b</title>
<list>4</list>
<list>5</list>
<list>6</list>
</main>
<m>
<title>c</title>
<list>7</list>
<list>8</list>
<list>9</list>
</m>
</xml>

So how do we read only the values in <main></main> <title></title>?

The php file (cy.php) is as follows:
Copy Code code as follows:

<?php
$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. " <br/> ";
}
$title = $main->getelementsbytagname ("title");
foreach ($title as $title)
{
$value = $title->firstchild->nodevalue;
echo $value. " <br/> ";
}
}

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

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

7
8
9

Think about, how to read out the value of <title></title> in <m>?

PHP reads XML and consolidates it with an example:

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

<?xml version= "1.0″encoding=" gb2312″?>
<LevelOne>
<LevelTwo>
<levelthree id= "1″>this is Text one</levelthree>
<levelthree id= "2″>this is Text two</levelthree>
<levelthree id= "3″>this is Text three</levelthree>
</LevelTwo>
<LevelTwo>
<levelthree id= "4″>this is Text four</levelthree>
<levelthree id= "5″>this is Text five</levelthree>
<levelthree id= "6″>this is Text six</levelthree>
</LevelTwo>
</LevelOne>

The php file (cy.php) is as follows:
Copy Code code as follows:

<?php
$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);//return the contents of the first Levelone node
$LevelTwo = $LevelOne->getelementsbytagname (' leveltwo '); Gets the node by name, returns all Leveltwo
The foreach ($LevelTwo as $Content)/loop reads out all Leveltwo, and in the loop, leveltwo the 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. " <br/> ";
echo $value. " <br/> ";
echo $id. " <br/> ";
}
}
?>

If you get the node in such a way as $levelone = $xml->getelementsbytagname (' Levelone '), then the contents of the read inside need to be looped with a foreach because $levelone = $xml-> getElementsByTagName (' Levelone ') returns, is a collection, not a specific node--only the node called Levelone only one ....
If you obtain a node in a manner such as $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 Code code as follows:

<?xml version= "1.0″encoding=" gb2312″?>
<xml>
<site>
<part id= "1″>
<title id= "a" >czbin XML forum </title>
<describe>xml related articles </describe>
</part>
<part id= "2″>
<title id= "b" >czbin PHP forum </title>
<describe>php related articles </describe>
</part>
<part id= "3″>
<title id= "C" >czbin Ajax forum </title>
<describe>ajax related articles </describe>
</part>
</site>
</xml>

The php file (cy.php) is as follows:
Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Sxml.xml ');
$part = $xml->site->part;
foreach ($part as $content)
{
echo $content [' ID ']. <br/> ";
echo $content->title. " <br/> ";
echo $content->title[' id ']. " <br/> ";
echo $content->describe. " <br/> ";
}
?>

cy.php Output Result:
1
Czbin XML Forum
A
Articles related to XML
2
Czbin PHP Forum

Related articles in PHP
3
Czbin Ajax Forum
C
Ajax-related articles

What do you think? It's really simple!

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.