473,439 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,439 software developers and data experts.

unable to view my file in browser

149 100+
hi,
i developed xml file..i want to view that xml file in browser...but im unable to do..it is showing in console..but i need is in browser..can any one help me out..here is my code..its urgent.


Expand|Select|Wrap|Line Numbers
  1. package xml;
  2.  
  3. import java.io.*;
  4.  
  5. import org.w3c.dom.*;
  6. import org.xml.sax.SAXException;
  7.  
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.xml.parsers.*;
  13. import javax.xml.transform.*;
  14. import javax.xml.transform.dom.*;
  15. import javax.xml.transform.stream.*;
  16.  
  17. public class XmlRead extends HttpServlet {
  18.  
  19.     private static PrintWriter out;
  20.  
  21.  
  22.  
  23.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  24.     throws ServletException, IOException {
  25.  
  26.         response.setContentType("text/html");
  27.         PrintWriter pw = response.getWriter();
  28.         String s = null;
  29.  
  30.         try {
  31.             DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  32.             DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
  33.             File xmlfile = new File("E:/xgprs/xml/WebContent/MyXml.xml");
  34.             Document doc = null;
  35.  
  36.             if (xmlfile.exists()) {
  37.                 try {
  38.                     doc = docBuilder.parse(xmlfile);
  39.                 } catch (Exception ex) {
  40.                     doc = docBuilder.newDocument();
  41.                     Element root = doc.createElement("company");
  42.                     doc.appendChild(root);
  43.                 }
  44.             } else {
  45.                 doc = docBuilder.newDocument();
  46.                 // Creating Root Tag
  47.                 Element root = doc.createElement("company");
  48.                 doc.appendChild(root);
  49.             }
  50.  
  51.             // creating a new instance of a DOM to build a DOM tree.
  52.             // Document doc = docBuilder.newDocument();
  53.             readdata(xmlfile, doc );
  54.             pw.println("<b>Xml File Viewed Successfully</b>");
  55.         } catch (Exception e) {
  56.             System.out.println(e);
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.  
  61.  
  62.  
  63.     public static void readdata(java.io.File file,Document doc) throws Exception {
  64.         TransformerFactory transformerFactory = TransformerFactory.newInstance();
  65.         Transformer transformer = transformerFactory.newTransformer();
  66.         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  67.  
  68.         StringWriter writer = new StringWriter();
  69.         StreamResult result = new StreamResult(writer);
  70.         DOMSource source = new DOMSource(doc);
  71.         transformer.transform(source, result);
  72.         String xmlString = writer.toString();
  73.         System.out.println(xmlString);
  74.  
  75.         /*FileInputStream finp=new FileInputStream(file);
  76.         byte b;
  77.         do
  78.         {
  79.             b=(byte)finp.read();
  80.             System.out.print((char)b);
  81.           }
  82.         while(b!=-1);
  83.         finp.close();
  84.         }
  85.  
  86.  
  87.  
  88.  
  89.         /*BufferedReader bw = new BufferedReader(new InputStreamReader(
  90.                 new FileInputStream(file)));
  91.         bw.readLine();
  92.  
  93.         //bw.read(xmlString);
  94.         //bw.flush();
  95.         bw.close();*/
  96.     }
  97.  
  98. }
  99.  
  100.  
thanks,
madhu.
Jul 20 '10 #1
1 1555
chaarmann
785 Expert 512MB
You wrote "It's urgent", so most likely you already found the solution yourself or you don't need it anymore.
But I'll write it down for the benefit of others anyway.

Your program prints to console and not to the webbrowser, because you are calling method "readdata" (line 53) which prints out the xmlString to console (line 73).
What you need to do is to return this string from the method and let it print through your PrintWriter pw.
That means apply following changes:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. String xmlString=readdata(xmlfile, doc ); 
  3. pw.println(xmlString); 
  4. ...
  5. public String readdata(...
  6. ...
  7. return xmlString;
  8. ...
Jul 23 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dan Stromberg | last post by:
Are there any reasonably secure file browser implementations for unix/linux (and perhaps MacOS X and Windows as well) written in python, that are accessed over https via a cgi script? Thanks.
2
by: Lyn | last post by:
What is the best way to import a picture (bmp format) via VBA from a Windows file and embed it into a Bound Object Frame (and thence to an OLE Object in a table)? I found an MS knowledge base...
1
by: dw | last post by:
Hello, all. I have a site that works fine when viewed in IE apart from the project in Visual Studio .NET 2003. However, when I right-click and do View in Browser, it tries to open it in the wrong...
2
by: | last post by:
I like the "view in browser" feature of vs.net: right click from an aspx page, and preview a working version of the page. I don't like how this can't be launched from the pages' codebehind. The...
3
by: Frank | last post by:
Hi, I'm experimenting with aspx. When I build a form with code-behind code it's possible to run with a mouse right-click and menu 'view in browser'. A browser window is opened within VB.NET. Works...
5
by: jfinnell | last post by:
Hi I'm working with the beta version of this product. I place the web in my master page, then link to my other pages. Set them up. When I select view in browser to check what I have done...
0
by: Scott | last post by:
I ran into a recent issue with a simple databinding code in VS.NET 2003, putting an ArrayList in an Application variable and binding it in a web form. Global.asax, Application_Start event: ...
4
by: Peter Larsen | last post by:
Hi, I want to be able to show a file browser embedded on a form - a browser which is similar to the browser you get by SHBrowseForFolder(). Is that possible ? Thank you in advance. BR Peter...
1
by: Amil Hanish | last post by:
I use the VS 2005 option "View in browser" to often view my web site. If I have a link in there like: <img src="/graphics/myimage.jpg"> This doesn't work even though the image is really...
0
by: Amil Hanish | last post by:
Earlier I had inquired as to why a normal <a hreftag didn't work when using the VS hosted site using a random local port. The more I think about it, this seems very wrong. Using the random port...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.