PHP obtains the node value in XML.

Source: Internet
Author: User

PHP obtains the node value in XML.
  
XML: test_xml2.xml
  
<? XML version = "1.0" encoding = "UTF-8"?>
  
<Friends_get_response list = "true" xmlns = "http://api.xiaonei.com/1.0/" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  
Xsi: schemalocation = "http://api.xiaonei.com/1.0/ http://api.xiaonei.com/1.0/xiaonei.xsd">
  
<Uid> 200032219 </uid>
  
<Uid> 84525914 </uid>
  
<Uid> 12345689 </uid>
  
<Uid> 98765432 </uid>
  
</Friends_get_response>
  
PHP code 1:
  
<? PHP
  
$ Doc = new domdocument ();
  
$ Doc-> load ('test _ xml2.xml ');
  
$ Productprofile = $ doc-> getelementsbytagname ('ds DS _ get_response ');
  
Echo '<PRE/> ';
  
Foreach ($ productprofile as $ profile)
  
{
  
// $ Productnames = $ profile-> getelementsbytagname ("uid ");
  
// $ Productname = $ productnames-> item (0)-> nodevalue;
  
// Echo $ productname;
  
Echo $ profile-> nodevalue;
  
}
  
?>
  
Result: In this way, all values in the node are obtained directly.
  
200032219
  
84525914
  
12345689
  
98765432
  
PHP Code 2:
  
<? PHP
  
$ Doc = new domdocument ();
  
$ Doc-> load ('test _ xml2.xml ');
  
$ Xiaonei = $ doc-> getelementsbytagname ("friends_get_response ");
  
$ I = 0;
  
Foreach ($ Xiaonei as $ key ){
  
$ Uid = $ key-> getelementsbytagname ("uid ");
  
Foreach ($ UID as $ PARAM ){
  
Echo $ param-> nodevalue. '<br/> ';
  
}
  
}
  
?>
  
Result: the value of the friends_get_response-> uid node is obtained.
  
200032219
  
84525914
  
12345689
  
98765432
  
Xml2: test_xml4.xml
  
<? XML version = '1. 0' standalone = 'Yes'?>
  
<Test>
  
<A> AA </a>
  
<B>
  
<BB> bb1 </BB>
  
<BB>
  
<BBB> bbb1 </BBB>
  
<BBB> bbb2 </BBB>
  
</BB>
  
</B>
  
<C> CC </C>
  
<D>
  
<F> FF </F>
  
</D>
  
</Test>
  
PHP code:
  
<? PHP
  
Echo '<PRE/> ';
  
$ Doc = new domdocument ();
  
$ Doc-> load ('test _ xml4.xml ');
  
$ Xiaonei = $ doc-> getelementsbytagname ("test ");
  
Foreach ($ Xiaonei as $ V)
  
{
  
Echo $ V-> nodevalue. '<br/> ';
  
}
  
?>
  
Result: the value of the multi-layer sub-node under the test node is obtained directly using nodevalue.
  
AA
  
Bb1
  
Bbb1
  
Bbb2
  
CC
  
FF
  
/***************************************/
  
PHP code:
  
<? PHP
  
$ Xmlstr = <XML
  
<? XML version = '1. 0' standalone = 'Yes'?>
  
<Movies>
  
<Movie>
  
<Title> PHP: Behind the parser </title>
  
<Characters>
  
<Character>
  
<Name> MS. coder </Name>
  
<Actor> onlivia actora </actor>
  
</Character>
  
<Character>
  
<Name> mr. coder </Name>
  
<Actor> El Act & #211; r </actor>
  
</Character>
  
</Characters>
  
<Plot>
  
So, this language. It's like, a programming language. Or is it
  
Scripting language? All is revealed in this thrilling horror Spoof
  
Of a documentary.
  
</Plot>
  
<Rating type = "thumbs"> 7 </rating>
  
<Rating type = "Stars"> 5 </rating>
  
</Movie>
  
</Movies>
  
XML;
  
$ Xml = simplexml_load_string ($ xmlstr );
  
Echo $ XML-> movie [0]-> title;
  
Echo '<br> ';
  
$ Arr = $ XML-> movie [0]-> characters [0]-> character;
  
Echo '<br> ';
  
Foreach ($ arr as $ KK => $ vv)
  
{
  
Echo $ VV-> name;
  
Echo '<br/> ';
  
}
  
?>
  
Result:
  
$ XML-> movie [0]-> title:
  
PHP: Behind the parser
  
$ VV-> name: Ms. coder
  
Mr. coder

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.