dom - PHP - Trying to display xml tag(tags with same name) nodevalue -


I am trying to display information from an XML file. I am using PHP and its DOMDocument class. I'm attaching the xml file and php code i have tried

  & lt ;? XML version = "1.0" encoding = "utf-8"? & Gt; & Lt; Services & gt; & Lt; Services & gt; & Lt; Type & gt; Wash & lt; / Type & gt; & Lt; Title & gt; Wash1 & lt; / Title & gt; & Lt; Content & gt; Full External Hand Wash & lt; / Content & gt; & Lt; Content & gt; Chamois dry, pressure clean wheels, all doors jazz & lt; / Content & gt; & Lt; Content & gt; Plus Free Type Gloss & lt; / Content & gt; & Lt; Price & gt; Hatch / sedan: $ 15 & lt; / Pricing & gt; & Lt; Price & gt; Other: $ 20 & lt; / Pricing & gt; & Lt; / Services & gt; & Lt; Services & gt; & Lt; Type & gt; Wash & lt; / Type & gt; & Lt; Title & gt; Wash2 & lt; / Title & gt; & Lt; Content & gt; Wash1 & lt; / Content & gt; & Lt; Content & gt; Plus vacuum & lt; / Content & gt; & Lt; Content & gt; Glass Clear (IN / OUT) & lt; / Content & gt; & Lt; Content & gt; Plus free internal wipe & lt; / Content & gt; & Lt; Price & gt; Hatch / sedan: $ 30 & lt; / Pricing & gt; & Lt; Price & gt; Other: $ 38 & lt; / Pricing & gt; & Lt; / Services & gt; & Lt; / Services & gt;  

php code ...

  & lt ;? Php header ('content-type: text / html; charset = UTF8'); $ Xmlfile = new DOMDocument (); $ Xmlfile-> Load ('services.xml'); $ Services = $ xmlfile-> GetElementsByTagName ('service'); Foreign Currency ($ Service $ as Service) {$ titles = $ Service-> GetElementsByTagName ('title'); $ Title = $ title- & gt; Items (0) - & gt; Node vela; Echo "

Title = / h1>"; $ Value = $ xmlfile-> GetElementsByTagName ('price'); Foreign Currency ($ value as value $) {$ value = $ value- & gt; Items (0) - & gt; Node vela; Echo "& lt; h1> gt; value & lt; / h1 & gt;"; }}? & Gt;

I'm just trying to output value tag values, because if it displays content then there should be no problem. Eventually, I will show these values ​​one at a time in the HTML structure.

Edit: At the moment the output of this code

  wash 1 hatch / sedan: $ 15 hatch / sedan: $ 15 hatch / sedan: $ 15 hatch / sedan: $ 15 Wash 2 Hatch / sedan: $ 15 hatch / sedan: $ 15 hatch / sedan: $ 15 hatch / sedan: $ 15  

You will get two bugs. As mentioned in another answer, you are receiving $ value from the wrong element:

  $ value = $ xmlfile-> GetElementsByTagName ('value');  

... should be:

  $ value = $ service-> GetElementsByTagName ('price');  

This does not completely fix the problem, however, because there is another problem you are getting through the prices, but then always the first & lt; Price & gt; Returning . You need to change it:

  $ value = $ value-> Items (0) - & gt; Nodeville;  

... on:

  $ value = $ value-> Nodeville;  

The correct code is then done:

  & lt ;? Php header ('content-type: text / html; charset = UTF8'); $ Xmlfile = new DOMDocument (); $ Xmlfile-> Load ('services.xml'); $ Services = $ xmlfile-> GetElementsByTagName ('service'); Foreign Currency ($ Service $ as Service) {$ titles = $ Service-> GetElementsByTagName ('title'); $ Title = $ title- & gt; Items (0) - & gt; Node vela; Echo "

Title = / h1>"; $ Value = $ service-> GetElementsByTagName ('price'); Forex currency ($ value as value $) {$ price = $ price- & gt; NodeValue; Echo "& lt; h1> gt; value & lt; / h1 & gt;"; }}

... which outputs:

wash 1

hatch / sedan: $ 15

Other: $ 20

Wash2

Hatch / sedan: $ 30

Other: $ 38


You have asked when it is necessary to use item () It basically depends on whether you (a domododist ) Or (or DOMNode ) Gives getElementsByTagName a DOMNodeList (can have many elements) so that you need to select a single, eg:

  $ Titles = $ Service-> GetElementsByTagName ('title'); // A 'Domoddlist' returns with all the titles $ title = $ titles-> Items (0) - & gt; Nodeville; // Only the first title  

you can repeat through a DOMNodeList (this applies traversal ). When you iterate you get a domonode :

  $ value = $ service-> GetElementsByTagName ('value'); // Returns a `domonodelt 'foreign currency ($ value as $ value) {// $ value a' domoded` // ...}  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -