• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

displaying xml file+additional tags

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a program to display the contents of an xml file to the console. Basically it just reads the whole xml file and spits it out to the console.

The xml file is as given below.

<?xml version="1.0"?>
<!DOCTYPE AICPCU_1 [
<!-- <!ATTLIST ITEM id ID #REQUIRED> -->
<!ATTLIST Header id ID #REQUIRED>
<!ATTLIST text id ID #REQUIRED>
]>

<COMPANY>
<Header id="Header" type="CLASSIC" >
..�..

<exam id="exam" examinfoversion="1.0">
.....
</exam>
<userinfo id="userid">

</userinfo>

</Header>

...�..
</COMPANY>


Now I'll have to dynamically insert data between
<userinfo id="userid"> and </userinfo> tags in the xml file, while displaying the output. Something like this:

<userinfo id="userid">
<name>XXXXXX</name>
<age>dshjadh</age>
<course>sdhasdhkj</course>
</userinfo>

There should not be any actual insertion of data in the xml file (the xml file is a read-only), however this data should be shown when the file contents are displayed as output.

How do I achieve this? I would really appreciate any help.

~Sara
[ February 08, 2006: Message edited by: Sara James ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could read the file into a DOM, use some DOM code to add the required nodes, and then write the DOM out to the console. Or you could use a SAX filter that reads the file and outputs the result, along with the extra nodes, to the console.

The DOM method is probably easier for beginners to understand. But you can read about both of them here:

http://www.cafeconleche.org/books/xmljava/chapters/index.html
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Paul.

But do you think I should be creating a temporary file for each of these cases ? I thought of doing something like this:

1) copy the contents of the original file to a temp xml file.
2) Create the additional nodes there.
3) Then output the contents to the console.

Is there a more efficient way of doing this?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I don't think you should be creating a temporary file. I think you should be using one of the two methods I described in my earlier post. Send your output directly to the console.
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for replying.

However, now I need to stream the output to the client using a ServletOutputStream. I am not sure what kind of input stream I should be using. This is what I did.

After having parsed through the xml file and added additional tags..

TransfomerFactory tf = TransfomerFactory.newInstance();
Transformer t = tf.newTransformer();
Source src = new DOMSource(doc); //Document object
// to print it out to file
Result dest = new StreamResult (new File("C:\\folder1\\temp.xml");
//to send it to console
//Result dest = new StreamResult(System.out);
t.transform(src, dest);

Then I save it to the temp file, and then use ServletOutputStream..

FileInputStream fis = new FileInputStream("C:\\folder1\\temp.xml");
ServletOutputStream out = response.getOutputStream();

byte[] buffer = new byte[64000];
int b = o
while((b = fis.read(buffer)) != -1)
out.write(buffer,0,b)

How can I get the formatted xml result to directly go to an input stream without creating a temp file?

Thanks.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are making it far too difficult. You have a ServletOutputStream, which is an OutputStream. You already know how to create a StreamResult from an OutputStream. So that's all you do.
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. Appreciate your help !
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,



Is a TransformerFactory required in this case?

Can I instead take the Document object as an Input Stream and write it out to the servletoutput stream? Something like this :



I tried using this but it obviously doesn't work - Method 'org.w3c.dom.document' not found in java.io.InputStream

The reason I'm trying to do this is - I'm reading other files using FileInputStream, and I'm trying to make my code modular by avoiding TransformerFactory. When I read a file , I do it this way..

ServletOutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream(filename);

Do you think it is possible? Thanks for helping - this is the first time I'm working on DOM.
[ February 14, 2006: Message edited by: Sara James ]
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need something to convert the DOM object into XML text. A Transformer is what you use to do that. And if you want the XML text to go to a particular place then it's easiest to have the Transformer send the text to that particular place.

You could instead have the Transformer write to a File or a String, and then copy from that File or String to the servlet response. But you can see that extra, unnecessary, work is being done in that case.

Making your code modular is an excellent idea, and you should definitely do that as much as possible, but as Einstein allegedly said, "everything should be as simple as possible, but no simpler". I know I said earlier that you were making things too difficult for yourself, but now you're making things too simple.
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
makes sense thanks !
 
bacon. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic