aspose file tools*
The moose likes XML and Related Technologies and the fly likes Problem parsing XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem parsing XML" Watch "Problem parsing XML" New topic
Author

Problem parsing XML

Arulmozhi Govindaraju
Greenhorn

Joined: Feb 20, 2013
Posts: 17
public class XMLFinal {
public static void main(String[] args)throws IOException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
String xmlString="<College><Dept>EEE</Dept></College>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document document = (Document) builder.parse( new InputSource(new StringReader(xmlString)));
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(document);
Result dest = new StreamResult( new File( "C:\\Data\\college.xml" ) );
aTransformer.transform(src,dest );
System.out.println("File Saved!!!");
}
}


when i run this program i got this error
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl cannot be cast to javax.swing.text.Document
at xmlconstruct.XMLFinal.main(XMLFinal.java:35)
please any one solve the problem?
Arulmozhi Govindaraju
Greenhorn

Joined: Feb 20, 2013
Posts: 17
please tell any one how to construct xml in java?
Arulmozhi Govindaraju
Greenhorn

Joined: Feb 20, 2013
Posts: 17
how to parse the string and how construct the xml
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
Welcome to the Ranch
Your last two posts make you look very impatient.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Arulmozhi Govindaraju wrote:Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl cannot be cast to javax.swing.text.Document
at xmlconstruct.XMLFinal.main(XMLFinal.java:35)
please any one solve the problem?


That's because your Document variable is a javax.swing.text.Document variable. I assume you imported that class? You need to import org.w3c.dom.Document instead.
Arulmozhi Govindaraju
Greenhorn

Joined: Feb 20, 2013
Posts: 17
Paul Clapham wrote:
Arulmozhi Govindaraju wrote:Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl cannot be cast to javax.swing.text.Document
at xmlconstruct.XMLFinal.main(XMLFinal.java:35)
please any one solve the problem?


That's because your Document variable is a javax.swing.text.Document variable. I assume you imported that class? You need to import org.w3c.dom.Document instead.



Thanks Paul..for your input
Arulmozhi Govindaraju
Greenhorn

Joined: Feb 20, 2013
Posts: 17

how to parse string to xml in java
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

how to parse string to xml in java

You can use Apache Commons Digester
digester.parse(new StringReader(s));

or

You can also use XStream - fromXML(), toXML()


Thanks,
Tushar (SCJP 1.5)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem parsing XML