This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Firstly, i would like to add that i am a Java novice and I was wondering if someone could help me please with regards to the following query i have.
I have created a programme which uses DOM parser to parse an XML document; I then use Xpath to query the document which i then update using .setTextContent. Finally, i write this out to a new file.
All this works fine if its all within the Main Class however when i try to pass the XML document (object) between different classes i create, it doesnt seem to work.
Here is a snippet of the code which works successfully ...
And here is a snippet of the Organisation Class i have created were i call the different text values to be used within my XML (I have just listed the Organisation code in this example):
As I said, the above approach works successfully however by programming it like this, the main class is going to be huge, mainly because I have quite a lot of Organisations to choose from; there are also other nodes to be implemented along with Organisation.
What i thought would be a better approach would be to have the Xpath snippet included within the Organisation Class and just call the updated XML document or object back into the main class once all changes had been updated however this is were i am having the problem.
When i try and pass my parsed XML document / object (in this case called 'doc') to the Organisation Class and then back, i seem to be hitting all sorts of problems - i have tried passing both 'Document' and 'Object' to no avail (although this could just be me doing it wrong as i have only ever passed a String or Int).
I could list the different ways i have tried however it may be quicker for someone to just give me advise on how to actually pass an XML document / object between classes, call the updated document / object back to the main class, and then use this updated document / object to print to a new file.
Apologies if this isnt clear and if its not, then please do let me know and i will try and elaborate more.
A reference to a DOM can certainly be passed around just like a reference to a String object or any other object, there is nothing magic about it.
I don't see where you say why you think it is not working - sometimes people get confused about the nature of a Document object reference because they try to do something like:
System.out.println("DOM reference is " + doc ); which prints "DOM reference is null"
This happens because the value of doc.toString() is null - see the Javadocs for org.w3c.dom.Node for a lovely table you should get very familiar with.
John Paul Hoey
Joined: Apr 15, 2012
Thanks for the reply.
The example you list in your response i.e. doc.toString() is null is actually very similar to the problem i am facing and yes, it is probably because i am getting confused about the nature of a Document object.
Here is the code i use within the main class to pass over the XML document to the Organisation_64319:
And within the Organisation_64319 class here is the code i return (after the updates have been provided to the XML document i.e. doc):
So when i pass over the doc and then try and create doc1, it does indeed give null values when i save the file.
Any idea how to get around this?
Ive had a look at the link you provided however i cant seem to find what im looking for.
but a simple return doc; if it's just going to return the object referred by doc.
Are you sure the document returned is null? Can you try calling the Document:getDocumentElement() method. Also verify if the initial doc is null or not.
John Paul Hoey
Joined: Apr 15, 2012
Ignore my last comment, i finally managed to figure out what the problem was. I was calling my initial methods (i.e String IdValue) to update the node however this wasnt the correct way to do it - all i had to do was use the exact value like so: