这篇文章主要为大家详细介绍了PHP读取XML值的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
用Php读取XML最简单的情况:代码如下:
<?xml version=”1.0″ encoding=”gb2312″?>
<xml>
<list>1</list>
<list>2</list>
<list>3</list>
</xml>
代码如下:
<?php
$xml = new DOMDocument();
$xml->load('cy.xml');
foreach($xml->getElementsByTagName('list') as $list)
{
$value = $list->firstChild->nodeValue;
echo $value.”<br />”;
}
?>
代码如下:
<?xml version=”1.0″ encoding=”gb2312″?>
<xml>
<main>
<list>1</list>
<list>2</list>
<list>3</list>
</main>
</xml>
代码如下:
<?php
$xml = new DOMDocument();
$xml->load('cy.xml');
foreach($xml->getElementsByTagName('list') as $list)
{
$value = $list->firstChild->nodeValue;
echo $value.”<br />”;
}
?>
代码如下:
<?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>
代码如下:
<?php
$xml = new DOMDocument();
$xml->load('cy.xml');
foreach($xml->getElementsByTagName('list') as $list)
{
$value = $list->firstChild->nodeValue;
echo $value.”<br />”;
}
?>
代码如下:
<?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>
代码如下:
<?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 />”;
}
}
?>
代码如下:
<?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>
代码如下:
<?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 />”;
}
}
?>
代码如下:
<?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>
代码如下:
<?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 />”;
}
}
?>
代码如下:
<?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>
代码如下:
<?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 />”;
}
}
?>
代码如下:
<?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>
代码如下:
<?php
$xml = new DOMDocument(); //建立一个DOMDocument
$xml->load('cy.xml'); //Php指定需要读取xml文件的位置
$LevelOne = $xml->getElementsByTagName('LevelOne');//按照名称取得节点,返回所有节点的集合,不过这里这样读LevelOne是没有意义的….
$LevelOne = $xml->getElementsByTagName('LevelOne')->item(0);//返回第一个LevelOne节点中的内容
$LevelTwo = $LevelOne->getElementsByTagName('LevelTwo'); //按照名称取得节点,返回所有LevelTwo
foreach ( $LevelTwo as $Content )//循环读出所有LevelTwo,并在循环里,把LevelTwo用Content表示
{
$LevelThree = $Content->getElementsByTagName('LevelThree');//返回所有LevelThree
foreach ( $LevelThree as $Concert )
{
$name = $Concert->nodeName;//节点名称
$value = $Concert->nodeValue;//节点值
$id = $Concert->getAttribute('id');//”id”属性值
echo $name.”<br />”;
echo $value.”<br />”;
echo $id.”<br />”;
}
}
?>
代码如下:
<?xml version=”1.0″ encoding=”gb2312″?>
<xml>
<site>
<part id=”1″>
<title id=”a”>czbin xml版块</title>
<describe>xml的相关文章</describe>
</part>
<part id=”2″>
<title id=”b”>czbin php版块</title>
<describe>php的相关文章</describe>
</part>
<part id=”3″>
<title id=”c”>czbin ajax版块</title>
<describe>ajax的相关文章</describe>
</part>
</site>
</xml>
代码如下:
<?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 />”;
}
?>
本文来自:http://www.q1010.com/173/13273-0.html
注:关于PHP读取XML值的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。