0

I am currently trying to search a local XML File to retrieve a certain attribute, my search key is also another attribute.

My XML is like this:

<JPPlatforms>
  <Platform PlatformTag="2980" PlatformNo="47280">
  </Platform>
<JPPlatforms>

I want to search the 1000 or so lines for the PlatformNo 47280 and return the PlatformTag 2980.

How would I implement this in Java for Android? Should I use an XMLPullParser is there something more efficient to find and return one particular value in XML?

ForeverLearning
  • 1,027
  • 1
  • 12
  • 30

2 Answers2

1

This is trivial if you can use XPath :

//Platform[@PlatformNo=47280]/@PlatformTag

Explanation :

  • //Platform : find Platform element, anywhere in the XML document...
  • [@PlatformNo=47280] : ...where PlatformNo attribute value equals 47280
  • /@PlatformTag : from such Platform, return PlatformTag attribute

I haven't used XPath in Android, but it seem possible according to this thread : Search in XML File with XPath in Android

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
0

Try XMLReader.

Here is the examples of usage:
https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194