Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

build DOM Object of a file whose path is given

 
s ithu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with this code? java-xml-dom..






getting output as
[#document: null]


Additional Details
I am new in xml .Actually i am trying to build DOM Object of a file whose path is given .So that it can be used in display ,edit, traverse functions...
Problem is that variable doc is getting a null value.Why is it so?
 
Santhosh ayiappan
Ranch Hand
Posts: 80
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cant directly print the Document object. You have to use StringWriter to convert it to String.

Regards
Santhosh
 
s ithu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santhosh ayiappan wrote:You cant directly print the Document object. You have to use StringWriter to convert it to String.

Regards
Santhosh



Thanks Santhosh for your answer.
But still why variable doc getting a null value?
How can I correct it?
 
Santhosh ayiappan
Ranch Hand
Posts: 80
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the method that would return the String by taking the Document object as input.



Regards
Santhosh
 
s ithu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santhosh ayiappan wrote:Below is the method that would return the String by taking the Document object as input.



Regards
Santhosh



Thank you again
In oder to run above method we need variable doc isn't it?
But i am getting null value in variable doc .


 
Santhosh ayiappan
Ranch Hand
Posts: 80
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you tried to execute the code ?
 
Santhosh ayiappan
Ranch Hand
Posts: 80
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Execute the code and share the output/errors if any....
 
s ithu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santhosh ayiappan wrote:Execute the code and share the output/errors if any....






When I run this code I got this error.

run: Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl cannot be cast to javax.swing.text.Document at common.parseXmlFile.parseXmlFile(parseXmlFile.java:28) at common.parseXmlFile.main(parseXmlFile.java:59) Java Result: 1

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



That error is telling you that you have the wrong kind of Document.

In order to create a DOM, you want to be using the classes in org.w3c.dom.

A swing.text.Document is just a text container.

Bill

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ithu wrote:Problem is that variable doc is getting a null value.Why is it so?


That is simply because the "value" of a Document node is null. Check out the API documentation of org.w3c.dom.Node to confirm that. But you aren't going to be using the "value" of the Document node anyway, so there is no need to spend any time worrying about it. It isn't a problem.
 
s ithu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys.....


I replaced javax.swing.text.Document with DOM

Now I can use doc to traverse the file right?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have succeeded in creating a org.w3c.dom.Document DOM then you can use the methods in the standard library to "navigate" around in the DOM.

You can also use XPath, which can give you a convenient way of expressing navigation in a DOM, and XSLT which can be convenient for creating new documents from the DOM.

Harolds free online book has loads of examples.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic