0

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!

Jasan Steele
  • 75
  • 1
  • 9

1 Answers1

0

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

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

Refer this article for how to use XPath in Java.

K139
  • 3,654
  • 13
  • 17
  • Unfornutanetly still didnt get it to work My Code: URL url = new URL("http://example.com/example.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(url.openStream()); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("some expression"); String res = expr.evaluate(doc); But no matter what I dont get anything returned :S – Jasan Steele May 06 '15 at 19:44