1

I have an XML file and want to read the information using XPath, I want to read the 'listings_Id' and 'budget_remaining' together.

XML example

<ads>
 <ad>
   <listing_ids>
     <listing_id>2235</listing_id>
     <listing_id>303</listing_id>
     <listing_id>394</listing_id>
  </listing_ids>
  <reference_id>11</reference_id>
  <net_ppe>0.55</net_ppe>
  <budget_remaining>50000.0</budget_remaining>
</ad>
<ad>
   <listing_ids>
     <listing_id>2896</listing_id>
   </listing_ids>
   <reference_id>8</reference_id>
   <net_ppe>1.5</net_ppe>
   <budget_remaining>1.3933399</budget_remaining>
 </ad>
</ads>

I want to output it to a CSV file as the following

ListingId,BudgetRemaining
2235,0.55
303,0.55
394,0.5
2896,1.5

I am trying to use the example as

  DataReader reader = new XmlReader(new File("links.xml"))
    .addField("ListingId", "//ad/listing_ids/listing_id")
    .addField("BudgetRemaining", "//ad/budget_remaining")
    .addRecordBreak("//ad")
    .setExpandDuplicateFields(true);

But it seems so I cannot find the jar file for XMLReader and DataReader and also I am going definitely wrong with the format. New to Java, please any help is appreciated.

user3188390
  • 603
  • 2
  • 11
  • 19
  • I'm not sure what tutorial you followed / where you copied that code from, but that's not the default Java XPath implementation you're trying to use. Have a look at http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java?rq=1. – Jens Erat Feb 19 '14 at 21:28
  • @JensErat : I followed the same example, it is an Xmlreader, but I cannot find the jar file for it. – user3188390 Feb 19 '14 at 21:30
  • Just realized, it's the third answer. The library is linked just above the code! Anyway, if there's no special reason (the possibility of directly outputting CSV might be one, I don't know that library) you might consider going for the first answer and just use the "standard Java way" of doing the stuff. – Jens Erat Feb 19 '14 at 21:35

1 Answers1

1

You're following a tutorial for a commercial library ("Data Pipeline"), which is not shipped with the JDK and needs to be installed separately. Get if from the Download page and install it using their Getting Started manual.

Now, the classes should be found.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96