Method 1 for reading xml from php --- reading xml from DOMDocument
XML (Extensible Markup Language) can be used to expand the Markup Language. like HTML, it is SGML (Standard Generalized Markup Language, Standard common Markup Language ).
- Xml source file
- Zhang Ying
- Male
- 28
- Tank
- Male
- 28
- $ Doc = new DOMDocument ();
- $ Doc-> load ('person. XML'); // read the xml file
- $ Humans = $ doc-> getElementsByTagName ("humans"); // get the object array of the humans tag
- Foreach ($ humans as $ human)
- {
- $ Names = $ human-> getElementsByTagName ("name"); // an array of objects that obtain the name tag
- $ Name = $ names-> item (0)-> nodeValue; // obtain the value in node, as shown in figure
- $ Sexs = $ human-> getElementsByTagName ("sex ");
- $ Sex = $ sexs-> item (0)-> nodeValue;
- $ Olds = $ human-> getElementsByTagName ("old ");
- $ Old = $ olds-> item (0)-> nodeValue;
- Echo "$ name-$ sex-$ old \ n ";
- }
- ?>
|