Solved

How to convert Document object to Input Source ?

Posted on 2001-06-16
7
268 Views
Last Modified: 2010-07-27
Hi all,

I am having a XML document objecy created on the fly. I need to validate it against Schema. I am using xerces 1.4.0. I have set features for the parser.Now i need to parse to validate the XML.

For this i need to call "parser.parse()". But parse() method takes "InputSource" as parameter. But i have Document object. How do i convert this Document object to "InputSource" for passing it to parse() method.

Can anybody help.

Best Regards,
Prasanna
0
Comment
Question by:Prasanna23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 75 total points
ID: 6198146
Get your Document object as a String.
The quickest way I can think of doing this off the top of my head is :
  Element e = document.getDocumentElement();
  String s = e.toString();
Then get the String as a byte array.
  byte[] b = s.getBytes();
Then create a BtyeArrayInputStream using the byte array.
  BtyeArrayInputStream bais = new BtyeArrayInputStream(b);
Then create your InputSource from the ByteArrayInputStream.
  InputSource is = new InputSource(bais);
0
 
LVL 6

Expert Comment

by:wbradney
ID: 6198206
Prasanna23,

You need to serialize the Document to a string (or maybe a file) using XMLSerializer(StringWriter, OutputFormat), and then construct a new InputSource from the string (or file) to pass to the parser using InputSource(StringReader).

Regards,
WMB
0
 

Author Comment

by:Prasanna23
ID: 6198225
Hi ozymandias,
I tried the above method. Technically it looked fine. But when i tried to print the String got using e.toString() it just printed "[response:null]". "response" is my root element. I checked up my Document object it is not null.

Will Element.toString() work directly. I am toying over idea of XMLSerializer to get String. I have a slight doubt about Element.toString(). Please confirm about Element.toString() or is there any other method to convert it to String.

Thanks and Best Regards,
Prasanna
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:Prasanna23
ID: 6198256
Hi,
Thanks to both of u. But i dont know whose answer to accept. Both are correct. I am in a fix. I couldnt find provision to accept both answers. What do i do?????

Thanks and Best Regards,
Prasanna
0
 

Author Comment

by:Prasanna23
ID: 6198263
Thanks WMB & Ozymandias

Best Regards,
Prasanna
0
 
LVL 6

Expert Comment

by:wbradney
ID: 6198264
Prasanna23,

Element.toString() will not return the XML representation of the Element. To produce XML, you need to know the output type and output format (encoding), and XMLSerializer provides that functionality.

Here's an example:
import java.io.*;
import org.apache.xerces.parsers.*;
import org.apache.xml.serialize.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class ElemString
{
     public static void main(String[] args)
     {
          try
          {
               DOMParser parser = new DOMParser();
               parser.parse(args[0]);
               Document doc = parser.getDocument();

               System.out.println("Trying toString()...");
               Element elem = doc.getDocumentElement();
               String xml = elem.toString();
               System.out.println(xml);
               System.out.println("Doesn't work!");
               
               System.out.println("Trying serialize...");
               StringWriter sw = new StringWriter();
               XMLSerializer ser = new XMLSerializer(sw, new OutputFormat("xml", "UTF-8", true));
               ser.serialize(doc);
               xml = sw.toString();
               System.out.println(xml);
               System.out.println("Looks good!");
               
               parser.parse(new InputSource(new StringReader(xml)));
               System.out.println("Parsed OK!");
          }
          catch (Exception e)
          {
               e.printStackTrace();
          }
     }
}


Regards,
WMB
0
 

Author Comment

by:Prasanna23
ID: 6199424
Thanks wmb,

I have done the same thing. I have used setOutputStream instead.

Thanks and Best Regards,
Prasanna
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Load XML file into a SQL Server Table using SSIS 2012 9 104
Powershell import xml to array (non-cli xml) 4 36
Need a modeling tool 2 42
parsing xml using powershell 6 35
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question