How PHP reads the node content of XML two examples

Source: Internet
Author: User

A first example:

PHP is closely related to XML, the following is a simpler example of parsing XML node data, the sample custom has an XML sample file code, through PHP will read out the XML node in the ID, name and email data, is the premise of reading a large XML file, the novice words , you should also refer to the following:

<?PHP02//defining an XML sample file03$xml _string="<?xml version= ' 1.0 '? >04 <users>05 <user id= ' 398 ' >06 <name>foo</name>07 <e Mail>[email protected]</name>08 </user>09 <user id= ' 867 ' >10 <name>foobar</name>1 1 <email>[email protected]</name>12 </user>13 </users>";14//Start parsing XML15$xml=simplexml_load_string($xml _string);16foreach($xml->user as $user)17    {18Echo $user[' ID '], ';19Echo $user->name, ";20Echo $user->email, ' <br/> ';21st    }?>

A second example:

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

The XML file (CY.XM) is 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:
<?php
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
$main = $xml->getelementsbytagname_r (' main ');
foreach ($main as $main)
{
$list = $main->getelementsbytagname_r ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
}
$m = $xml->getelementsbytagname_r (' m ');
foreach ($m as $m)
{
$list = $m->getelementsbytagname_r ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
}
?>

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:
<?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:
<?php
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
$main = $xml->getelementsbytagname_r (' main ');
foreach ($main as $main)
{
$list = $main->getelementsbytagname_r ("list");
foreach ($list as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " <br/> ";
}
$title = $main->getelementsbytagname_r ("title");
foreach ($title as $title)
{
$value = $title->firstchild->nodevalue;
echo $value. " <br/> ";
}
}

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

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

Think about it, how do I read the value of <title></title> in <m>?

PHP reads XML and reinforces it with an example:

The XML file (Cy.xml) is as follows:
<?xml version= "1.0" encoding= "gb2312"?>
<LevelOne>
<LevelTwo>
<levelthree id= "1" >this is textone</levelthree>
<levelthree id= "2" >this is texttwo</levelthree>
<levelthree id= "3" >this is textthree</levelthree>
</LevelTwo>
<LevelTwo>
<levelthree id= "4" >this is textfour</levelthree>
<levelthree id= "5" >this is textfive</levelthree>
<levelthree id= "6" >this is textsix</levelthree>
</LevelTwo>
</LevelOne>

The php file (cy.php) is 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_r (' Levelone ');//Get the node by name, return the collection of all nodes, but there is no point in reading Levelone here ....
$LevelOne = $xml->getelementsbytagname_r (' Levelone ')->item (0);//Returns the contents of the first Levelone node
$LevelTwo = $LevelOne->getelementsbytagname_r (' 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_r (' 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 the node is obtained in a way such as $levelone = $xml->getelementsbytagname_r (' 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_r (' 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:
<?xml version= "1.0" encoding= "gb2312"?>
<xml>
<site>
<part id= "1" >
<title id= "A" >czbinxml section </title>
Related articles in <describe>xml </describe>
</part>
<part id= "2" >
<title id= "B" >czbinphp section </title>
Related articles in <describe>php </describe>
</part>
<part id= "3" >
<title id= "C" >czbinajax section </title>
Related articles in <describe>ajax </describe>
</part>
</site>
</xml>

The php file (cy.php) is 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 Results:
1
Czbin XML section
A
Related Articles of XML
2
Czbin PHP Section
B
Related articles in PHP
3
Czbin Ajax Section
C
Articles related to Ajax

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

How PHP reads the node content of XML two examples

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.