File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes build DOM Object of a file whose path is given Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "build DOM Object of a file whose path is given" Watch "build DOM Object of a file whose path is given" New topic
Author

build DOM Object of a file whose path is given

s ithu
Greenhorn

Joined: Apr 10, 2012
Posts: 11
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

Joined: Jan 30, 2007
Posts: 80

You cant directly print the Document object. You have to use StringWriter to convert it to String.

Regards
Santhosh
s ithu
Greenhorn

Joined: Apr 10, 2012
Posts: 11
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

Joined: Jan 30, 2007
Posts: 80

Below is the method that would return the String by taking the Document object as input.



Regards
Santhosh
s ithu
Greenhorn

Joined: Apr 10, 2012
Posts: 11
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

Joined: Jan 30, 2007
Posts: 80

Did you tried to execute the code ?
Santhosh ayiappan
Ranch Hand

Joined: Jan 30, 2007
Posts: 80

Execute the code and share the output/errors if any....
s ithu
Greenhorn

Joined: Apr 10, 2012
Posts: 11
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

Joined: Mar 22, 2000
Posts: 12675
    
    5



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


Java Resources at www.wbrogden.com
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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

Joined: Apr 10, 2012
Posts: 11
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

Joined: Mar 22, 2000
Posts: 12675
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: build DOM Object of a file whose path is given
 
Similar Threads
Problem Overwriting XML Section
DOM (xml) question
problem in deleting the file
SAXParseException: XML document structures must start and end within the same entity.
Using a parsed XML document