Php domdocument parsing xml document instance

Source: Internet
Author: User
Tags foreach

// Parse xml instance 1
$ ObjDOM = new DOMDocument ();
$ ObjDOM-> load ("xml. xml ");
$ Parent = $ objDOM-> getElementsByTagName ("Product ");
Foreach ($ parent as $ value)
  {
$ Names = $ value-> getElementsByTagName ("Name ");
$ Name = $ names-> item (0)-> nodeValue;
   
$ Urls = $ value-> getElementsByTagName ("Url ");
$ Url = $ urls-> item (0)-> nodeValue;
   
$ Images = $ value-> getElementsByTagName ("Image ");
$ Image = $ images-> item (1)-> nodeValue;
  }
 
// DOMDocument instance 2
 
$ Xml = new DOMDocument ();
$ Xml-> load ("me. xml ");
$ PostDom = $ xml-> getElementsByTagName ("post ");
Foreach ($ postDom as $ post ){
$ Title = $ post-> getElementsByTagName ("title ");
Echo "Id:". $ title-> item (0)-> attributes-> item (0)-> nodeValue. "<br/> ";
Echo "Title:". $ title-> item (0)-> nodeValue. "<br/> ";
Echo "Details:". $ post-> getElementsByTagName ("details")-> item (0)-> nodeValue. "<br/> ";
}
 
?>
Xml. xml
<Root>
<Category>
<Product>
<Name> webpage creation tutorial </Name>
<Image>
<SizeName> Small </SizeName>
<Url> http://www.111cn.net </Url>
</Image>
<Image>
<SizeName> Medium </SizeName>
<Url> http://www.111cn.net </Url>
</Image>
<Image>
<SizeName> Large </SizeName>
<Url> http://www.111cn.net </Url>
</Image>
</Product>
</Category>
</Root>


Me. xm;

<? Xml version = "1.0" encoding = "UTF-8"?>
<Phplamp>
<Post>
<Title id = "1"> php xml Processing Overview 1 </title>
<Details> details 1 </details>
</Post>
<Post>
<Title id = "2"> php xml processing Introduction 2 </title>
<Details> details 2 </details>
</Post>
<Post>
<Title id = "3"> php xml processing Introduction 3 </title>
<Details> details 3 </details>
</Post>
</Phplamp>

Related Article

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.