PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi guys,
I asked this a short time ago but I got no answer. I hope that now there would be someone who could help.
Well, I have a .xml file which has a root tag and subtags. In each subtag there is an id. I need to get the last id of a subtag in the list.
This is a sample of my .xml
A lot depends on how large and complex your XML is possibly going to be. Options if small include things like preg_match_all. If larger/more complex than your example you may wish to look at actually processing your XML using something like SimpleXML or one of the other PHP XML libraries such as XML Parser Functions. The main trouble with XML parsing is to ensure that the original data is well formed, even if it not valid (matched to a specific Document Type Definition (DTD)).
Oren: I use Php 5.
CoderGoblin:Thank you for the reply. My XML is simple in structure the same as I posted and there can be one more child in it. The content may become a lot(list tags can be hundreds).
The next question you need to ask then is how am I getting this information in. Is it line by line or is a string. If you are reading line by line then it should be easy to use simply keep a running tag on every occurance of the list. If it is a string already I would use preg_match (personal preference). If the lists are always running from 1-x seqentially all you would need to do is count the occurances of the <list> tags. If not you need to work out a pattern to extract the last list attribute.
Thank you, CoderGoblin for your reply. Actually, I need to get the number of ids but although you explained me how to do it very well, I would appreciate it if you could post me a sample code on how to do it.