How to read an XML using Xmlreader in java? - java

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.

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.

Related

Java - How to extract information from many XML files in a directory and export it to an excel file

I am trying to get information from many xml files in a directory.
How can I get specific information from each one and send it to an excel file, in java?
file 1.xml
file 2.xml
file 3.xml
*********
**file.csv** or .**xls** with the information of the 'n' files XML
there are several libraries on Java that can help you to do so.
For instance, for getting information from XML you can use dom4j and extract the specific information make use of the query language XPATH, supported by the library (examples). And to read all the XML files form a directory, Java 8 has an easy way of achieving that.
Files.list(Paths.get("/path/to/xml/files"))
.map(YourXMLParser::parse)
.forEach(XLSExporter::export);
where parse method would have the signature:
public MyDataBean parse(Path path) {
InputStream inputStream = Files.newInputStream(Path);
SAXBuilder saxBuilder = new SAXBuilder(inputStream);
... <-- Making use of SAX for instance and return the read data in a custom Bean (MyDataBean)
}
As Files.list() method return Stream you can take advantage of that to use map and forEach.
Once you have the information from each XML files to you can export to XLS using the most used library in Java for it: Apache POI
I hope it can help.

How to save file with custom file extension in java?

Dear brothers Hope you all right?
I'm designing a document program, however, rather to save file .text extension or using any other MS-Office API in java, i want to create my custom file format such as ".sad" extension so that this sort of file can only be read by my programs, how this can be possible?
Your requirement seems ambiguous. Are you looking to make a program that creates MS Office Word documents or plain text files with a custom file extension?
In the case of the former, you can't have a custom extension as MS Word documents, by definition, have a .doc / .docx extension.
However, if you are looking to create a program that produces text files then you can easily have a custom extension. Just look at this tutorial: How to create a file in Java
I already stated why this is a bad idea. Yet I have a solution for you (more like a how-not-to-do-it)
Take your plain text you want to save, convert it to bytes and apply this "highly enthusiastic encryption nobody will ever be able to break" on it:
string plainText = "yadayada";
bytes[] bytesFromText = toBytes(plainText);
bytes[] encrypted = new Array(sizeof(bytesFromText)*2);
for(int i = 0; i < sizeof(bytesFromText); i++){
if((i modulo 2) == 0){
encrypted.push(toByte(Math.random modulo 255));
}
encrypted.push(bytesFromText[i]);
}
I let it up to you to figure out why this is a bad idea and how to decrypt it. ;)
You can create file with any extension
For example,
File f = new File("confidential.sad");
Hope this will work for you :)
Working with custom files in Java
Here is the tutorial that will help you in getting the concept about how to create your own files with custom extension such as .doc or .sad with some information embedded in it and after saving the file you want to read that information form the file.
ZIP
Similar applications often use archives to store data. Consider MS-Word and its documents >with the .docx file extension. If you change the extension of any .docx file to .zip, you >will find that the document is actually a zip archive, with only a different extension.
https://www.ict.social/java/files/working-with-custom-files-in-java-zip-archive
I have published a library that saves files, and handles everything with one line of code only, you can find it here along with its documentation
Github repository
and the answer to your question is so easy
String path = FileSaver
.get()
.save(file,"file.custom");

parse XML to POJO using XStream

I have an xml file which I want to read and parse it into POJO.
I am using XStream for this.
I am not able to send the file as an input to the code for parsing(file is on my local drive).
How to read the xml file and parse it using fromXML() method ?
I would be gratefull if someone can give example for sending xml file as input and parsing it to POJO and printing it on the screen
Thanks...
Based on the provided comments, I can better understand the problem you are facing. I believe the answer you are looking for is implicit collections. Check out the XStream tutorial on aliasing: http://x-stream.github.io/alias-tutorial.html .

jaudiotagger library - creating m4a tag reading file

I'm new to Java enviroment, recently I've been trying to create a simple mp3 tag reading file. Everything went ok until I tried to read tags from m4a file. After reading all API documentation and examples sadly I have still no idea how to scrypt m4a tag reading method. Could you please give me an example of working m4a tag reading method, so I could learn from it, with some simple clarification. I'd be very pleased.
Something like this:
AudioFile f = AudioFileIO.read("test.mp4");
Tag tag = f.getTag();
Iterator<TagField> i = tag.getFields()
while(i.hasNext()) {
System.out.println(i.next());
}
The problem you might be having is that mp3 can be read either using the generic interface which is the same for all formats (the above code is using that) or using the mp3 specific interface that deals with ID3 frames. So if you did mp3s using that interface then the code for the other formats is very different.

How do I (should I?) use Apache POI HWPFDocument?

I'm thinking about including the Apache POI into my application. Main goal is to output RTF document, but DOC would be nice, too. But the documentation is not very detailed about writing a HWPFDocument and everything I found on the web isn't helpful at all.
I can read DOC files, that's working without any problem. But I really can't see how I write a document. Maybe someone can give me a short code example?
Thanks a lot!
If you want to do RTF, These are text files and they are support in all versions of Word.
you can use itext for simple stuff
http://itextdocs.lowagie.com/tutorial/rtf/index.php
ro
you can export them the hard way
//-- save as example.doc -------------
{
\rtf1
\ansi
\ansicpg1252
\deff0
\deflang1033
{\fonttbl
{\f0
\fswiss
\fcharset0 Arial;
}
}
{
\*
\generator Msftedit 5.41.21.2500;
}
\viewkind4
\uc1
\pard
\f0
\fs20
Hello World
\par
}
Well,
It has been a long time since the last time I used POI. I read that the HWPFDocument is now orphaned (read on apache POI website). I would recommend using the WordML specification released by Microsoft instead.
http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
I have used this method before. The easiest way is to create a WordML template and just replace the values using XPATH

Categories