Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem parsing XML

 
Arulmozhi Govindaraju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell any one how to construct xml in java?
 
Arulmozhi Govindaraju
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to parse the string and how construct the xml
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Your last two posts make you look very impatient.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how to parse string to xml in java
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic