Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2007, 05:37 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in Coverting XML String into Document

Hi,

 I am getting some problem while parsing a String XML in to Document object.

CODE :

 public static Document converDocument(String xml){

 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = null;
try {
        builder = factory.newDocumentBuilder();
  } catch (ParserConfigurationException e) {
            e.printStackTrace();
  }
Document document = null;
try {
  document = builder.parse(new InputSource(new StringReader(xml)));
  System.out.println("Document is after parsing ====>>>"+document);
 } catch (SAXException e1) {
    // TODO Auto-generated catch block
  e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}
return document;

}

The xml which I am passing is :
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>abc</body>
</note>

It is giving the document as null;
Can anybody please help?

Regards,
sheeetal.


sheetal
__________________
sheetal
 
Old April 27th, 2007, 06:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're getting a problem, you say? What are its symptoms?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 27th, 2007, 06:44 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am getting the null value when i am trying to parse the XML String into Document Object.

Is there any other way to parse the XML into Document Object.

sheetal
 
Old April 27th, 2007, 06:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't think builder.parse() should ever return null. What implementation of DocumentBuilderFactory are you using?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 27th, 2007, 07:11 AM
Authorized User
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

From the following package i am using the DocumentBuilderFactory

javax.xml.parsers.DocumentBuilderFactory;


whatever the code i have written is it correct for covereting the xml into document


sheetal
 
Old April 27th, 2007, 09:15 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

javax.xml.parsers.DocumentBuilderFactory is an interface not a class. Please check what implementation you are using (use getClass().getName() on the result).

I can't see anything wrong with your code. But of course that doesn't mean it's right!

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass textbox string to Word document bookmark mii2029 ASP.NET 3.5 Basics 1 June 6th, 2008 01:13 PM
posting form data as XML string problem Cragdo XML 0 July 16th, 2007 10:03 AM
Coverting from SQL to Access aspless Classic ASP Databases 9 July 10th, 2007 04:07 AM
Problem retrieving string value of XML document wslyhbb Javascript How-To 0 September 19th, 2006 08:16 AM
XML Document Problem Ben Horne XML 5 November 19th, 2003 01:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.