1

I don't usually have to do any backend stuff, but for this one project I have to parse XML using PHP so it's all kind of new and very complicated to me. I need to display the fourth tag on a page and I figured I would use getElementsByTagName but the trouble is the three previous tags are the same, so it looks something like this:

<Person fname="John" lname="Smith"/> 
<Person fname="Frank" lname="Jones"/> 
<Person fname="Mike" lname="Jackson"/> 
<Person fname="Jack" lname="Williams"/> 
<value no="50"/> 
<value no="60"/> 
<value no="70"/> 

Here is what I would like to output in my HTML page using the first attribute in the fourth tag and the attribute in the second tag:

Mike: 60

Basically, is there any way I can request the value of the attribute in the fourth tag regardless of what the tag is or what comes before or after it?

Any help would be appreciated -- thanks!

Edit - I didn't write the XML, I request from a remote server.

2
  • How do you know you have to fetch the third Person element's fname attribute and associate it with the second value element's no attribute? Commented Jun 9, 2011 at 10:25
  • That's just a given... it's always that person, it's always that value. So I think what I'm really looking for is something similar to getElementById except instead of the id, it needs to fetch the fourth Person tag and the second Value tag, the structure of the document never changes, the only things liable to change are the values in the attributes... any ideas? Commented Jun 9, 2011 at 11:01

2 Answers 2

2

My notes about XML:

Links: http://php.net/simplexml.examples-basic

And example:

$xml='<p4wappremium>
    <servermessage>
        <providerref sid="123"/>
        <useractioninfo
            msisdn="48790300200"
            tid="12123123"
            stid="123123"
            pid="345345"
            bid="1"
          />
    </servermessage>
</p4wappremium>';

$xml = simplexml_load_string($xml);

foreach ($xml->providerref[0]->attributes() as $name -> $value) {
    ${$name}=$value;
}

foreach ($xml->useractioninfo[0]->attributes() as $name -> $value) {
    ${$name}=$value;
}

Hope it will be useful for your case.

Sign up to request clarification or add additional context in comments.

Comments

2

Well I would suggest you reorganize your "schema". Cause this looks pretty odd to me to associate tag-values by order, like you do.

why not

<Person fname="Mike" lname="Jackson" value="60"/>

or

<Person fname="Mike" lname="Jackson">
<value no="60"/> 
</Person>

instead?

Actually the way you apply those tags doesn't seem to be useful or maybe even not valid XML. B/c what you try to parse isn't an xml-dom-tree but a mere list. so why not write a list-parser yourself?

And if you want to use the DOM-extension and getElementsByTagName() then according to the manual you will get a DOMNodeList-object which allows you to refer to the resulting nodes by an index!?

2 Comments

I'm not writing the XML so I can't reorganize it, I'm grabbing it from a remote server...
I should also add that value isn't necessarily specific to that person, they just need to be concatenated for the purpose of this exercise...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.