<pko:POImport xmlns:xs="http://XMLSchema-instance"
  xmlns:pko="hello"
xs:schemaLocation=" PlannedQuantityImport.xsd" tag="TAG" notes="These are my notes" totalqtypctmin="2" totalqtypctmax="3" mismatchthresholdpct="8.0">
  <prodgrp id="100067">
    <prodid>SYC87948427320</prodid>
    <locgrp id="100067">
      <geoid>30454</geoid>
      <geoid>30982</geoid>
      <quantity date="2010-12-11" sequence="1" prodid="87948427320">600</quantity>
    </locgrp>
  </prodgrp>
</pko:POImport>

I want to get the data corresponding to

tag, notes,totalqtypctmin,totalqtypctmax,mismatchthresholdpct,prodgrp,prodid,locgroup,geoid etc.

3

There are 3 answers

0
Aniket Thakur On

You will need XML parser to extract information from a XML using your desired xPath. Refer to this Best XML parser for Java.

0
insomniac On

Their are various methods for parsing in java you could either use

  • DOM parsing
  • SAX parsing

These are included in the JDK by default you can also use JDOM Goto the following link to more info http://www.mkyong.com/tutorials/java-xml-tutorials/

0
shashwat On

I'd just use string matching.

inputStream = new BufferedReader(new FileReader("file.xml"));
while(inputStream.hasNext()){
    String s=inputStream.next();
    if(s.indexOf("totalqtypctmax")==-1)
        continue;
    String k = s.split("totalqtypctmax=\"")[1].split("\"")[0];
    System.out.println("The value of totalqtypctmax is "+k);
}

Java isn't my first language, so pardon any silly mistakes.

EDIT: The only reason I'm using string matching instead of a standard XML parser, which is obviously recommended for more elaborate XML files, is because, in this case, your requirements seem very specific.