This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

creating a DOM document from an XML file.

 
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, I've been workin on this for days now, and I decided to take a help from you guys.

my problem is:
I have a java class that uses the jasperreports API to create a report that reads a XML file as data source.

that's only the concept, what i need now, is to convert a XML file into a DOM document, the report needs a org.w3c.dom.document document, and i'm getting like to do it.

that's the try that matters now:

note: caminhoXML is a String defined before this, it holds the path for the XML file.


as you can see, it is a complete mess.
what really matter is this lines:


this println prints in the screen: [#document: null]
so that we can know that my parse method is returning a null document.

this null document turn out to be more errors in the report filling engine, and the report goes empty.


can you guys help me out on this one?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucas,
i think you do the right thing - exept printing the Document in the right manner. Following these instructions http://faq.javaranch.com/java/DocumentToString i modified your code to print out the Document properly:


Regards,
Georg
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see the documentation of the DocumentBuilder class, then you will find this

DocumentBuilder.parse(String uri)
Parse the content of the given URI as an XML document and return a new DOM Document object.



So basically it treats your xml document as a URI :P .
 
Giorgio Angelo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:If you see the documentation of the DocumentBuilder class, then you will find this

DocumentBuilder.parse(String uri)
Parse the content of the given URI as an XML document and return a new DOM Document object.



So basically it treats your xml document as a URI :P .



Lucas orginally used

DocumentBuilder.parse(File f)
Parse the content of the given file as an XML document and return a new DOM Document object.



but I dont think it makes any differnce because internally both are converted to an InputSource and do return the DOMDocument he needs:



respectively


 
Ankit Garg
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! I got confused. I thought caminhoXML contains the xml document as a String :P
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Georg Bruestle:
I appreciate the help! you are right about the printing, and your code printed the XML document properly.

Ankit Garg:
i'm sorry, if i showed you all the code that fills the caminhoXML variable it would be so much code, so i just hided it, that could cause some mess.


so............
yeah, looks like my printing was wrong, BUT, the thing is:

what does [#document: null] means?
'cuz now I see that the document object is not null, but in the report i keep getting warnings like:

and the report just get empty.

I was thinking, this line:

could get me some problems, because parametros is defined as:

and I really think that I'm getting some trouble in storing the document in this hashmap.

can the hasmap not store this object properly? so that when I try to read this document in other class, the document is null or not "well-formed"?

I really need you guys. thanks in advance for the help.
 
Giorgio Angelo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucas,
i'm not sure where your Problem actually is, but this works fine for me:



Perhaps you can provide some more information on what you are doing an where the problem may be.

Regards,
Georg
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Georg.
yeah it works for me too.

so, let me explain what am I doing to get this.

I'm using JasperReports API to compile,fill and export some reports.
i'm doing this since a lot of time, but this time i need to pass an XML datasource, not a connection, as i do usually.

for those who don't know jasper, it got a method fillreport(JasperReport jr, Map parameters, Connection conn)
and also a method fillreport(JasperReport jr, Map parameters, JRDataSource ds)

all I did was changing the last parameter, from connection to a datasource, OK jasper should do the work now.

but since I did this, i get empty reports, and a warning in the system output:


so, Jasper have an graphic interface to work with it, its caller iReport, its only an executable program and report editor that creates the jaspercode.

when I looked at iReport I noted a built-in parameter called "XML_DATA_SOURCE", then I looked at the class that was trowing the warning:


and I saw this:



thats when I related
JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT
with the XML_DATA_DOCUMENT parameter in iReport.

so, I used this parameters map (parametros) that is used to pass the params to jasper, to try setting the parameter that jasper warns to be null.

then i saw that the datatype for the parameter is org.w3c.dom.Document, and I tried to set the parameter he miss. then. i failed. and I looked for help in javaranch bigmoose saloon... and the end of the story you already know

I promess you guys, after I solve this, i pay you a round of beer.

 
Giorgio Angelo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unforunatelly I'm not used to JasperReport. But i found this site in their API:

http://jasperreports.sourceforge.net/api/constant-values.html#net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT

which prints that the constant JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT is a String: "XML_DATA_DOCUMENT".

Have you tried using this String in your parametros-HashMap? Or even better, using the constant itself?

I think here is someone doing something similar to your needs (Line 115-121):

http://kickjava.com/src/XmlDataSourceApp.java.htm

Good Luck,
Georg
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Georg

I saw the constant you said, and everything makes sense now.

now, we can know that in the JRXPathQueryExecuter class:

the getParameterValue method:

gets like this:


and then averything gets better, in a colorful pink world...

and that's my code now:


ok, let's see, i got a first print comand (line 1) that prints {}, saying that my HashMap is empty at the moment.

then, we got a second print (line 16), after the parametros hashMap is filled, it prints my hashmap using only a representation for the document:

then I used the lines that you writed(lines 18->26) to print the document that gets out of the hashmap (not the one obtained in the parsing, the one I take off the hashmap), and it turned out to be everything OK:

//i'm sorry for the XML structure to be on brazilian language, i wanted to keep the original file.
So, everything's fine huh?

but this line:

in the JRXPathQueryExecuter class keeps returning a null document.


listen, i thank you for all the help, I appreciate you spending your time helping me, I really need it working, and you represented a great step on making things work.


so Georg, i'm starting to think that it would be better for us to throw this into a jasper forum, so that now it turned out to be a jasper problem. are you with me?
if you have time and will to help me on discovering this one, I would appreciate.

regards.


(UPD: changed the data contained in the xml, it was personal data from one of our clients)
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, I solved it

seems like when I first solved the Dom document thing, all turned out to be a jasper problem, not things to discuss here.

if you got this same problem, you can try searching on jasperforge, there I find my answer.


thanks to you all guys! and to georg that helped me!

 
rubbery bacon. crispy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!