Search code examples
javaweb-servicesparsingdata-retrievalpattern-finding

How to get a parameter as a result of a search condition from a XML-File?


Im getting via Webservice an XML File and now I want just to get certain data out of it by finding a certain match in the same tag:

For example: < Person name="Peter" age="33" />

Now I want to find the name Peter and get his age returned. Maybe a general solution for "if this xml-tag contains this then give me that out of it". Im really not sure how to do that, maybe tokinizing it or something like that but i couldnt figure it out. Does someone have an idea? -- Code is written in Java!


Solution

  • Use XPath to extract the specific tag with a condition. Like

    "/Person[@name='Peter']/"
    

    Refer this article for how to use XPath in Java.