• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Xml/File Input/Output

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir ,
I have a xml file.But i need to change its contents.
How can i proceed this.The contents has to changed & saved.
Give me an idea regarding this.
with regards,
Sonara.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
U need to do the following steps

1.Parse the xml file using a parser like SAX,DOM,JAXP etc., and store in a Document(api) object.
2.Search the particular element u want to modify.
3.Again store the modified Document into an XML file.
This is the process to be followed to do your task..The name of APIs differ with respect to the parser, u are going to use.
Best Regards,
Paramaguru
 
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rahul..
You need to use DOM parser. Cuz you can't modify or add elements using SAX Parsers.
You can use JAXP available at http://java.sun.com/xml/xml_jaxp.html .
Also you can use many other DOM parsers to do the same task.
Thanks.
------------------
L Goundalkar
[email protected]
Sun Certified Programmer for Java 2 Platform
 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello paramaguru,
I am able to parse the contents using dom,search that particular content,but the modifications r not reflected in the file.
How come i proceed with modifying the contents & save a xml file.I have no idea of using xml file writer.
Excepting u'r reply,
Sonara.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i am not mistaken then JAXP is not a parser.
Hirdesh

Originally posted by Parama guru:
Hi,
U need to do the following steps

1.Parse the xml file using a parser like SAX,DOM,JAXP etc., and store in a Document(api) object.
2.Search the particular element u want to modify.
3.Again store the modified Document into an XML file.
This is the process to be followed to do your task..The name of APIs differ with respect to the parser, u are going to use.
Best Regards,
Paramaguru


 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rahul,
Here is the sample code to achieve your task..
Document document=new SAXReader().read("c:/student.xml");
Element students=document.getRootElement() ;
Element student=students.addElement("student");

Element id=student.addElement("id")
.addText("2");
Element name=student.addElement("name")
.addText("name2");
Element city=student.addElement("city")
.addText("city2");
FileOutputStream f1=new FileOutputStream("c:/student.xml");

XMLWriter writer=new XMLWriter(f1);
writer.write(document);

------------
// student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student>
<id>1</id>
<name>name1</name>
<city>city1</city>
</student>
<student>
<id>2</id>
<name>name2</name>
<city>city2</city>
</student>
</students>
Hope i fulfilled your request...
Best Regards,
Paramaguru

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have downloaded the xerces package.But in the above example given by our rancher the class SaxReader and XmlWriter are not avaliable there in the API.How can I run the above example?
One more thing is that I also tried this in IE5.5
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform">
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//name">
<xsl:sort order="ascending" select="."/>
<TR><TH><xsl:value-of select="."/></TH></TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="a.xsl"?>
<xslTutorial>
<name>John</name>
<name>Josua</name>
<name>Charles</name>
<name>Alice</name>
<name>Martha</name>
<name>George</name>
</xslTutorial>
But nothing is visible on the screen but in view source I could see this xml file.
Please help
payal sharma
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Payal, your example should work if you replace your
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform">
with
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mapraputa
I also want to ask what is server side tranformation and mu question-->
I have downloaded the xerces package.But in the above xml input example given by our rancher , class SaxReader and XmlWriter are not avaliable there in the API.How can I run the above example?
Payal sharma
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mapraputa
I also want to ask what is server side tranformation and my question-->
I have downloaded the xerces package.But in the above xml input example given by our rancher , class SaxReader and XmlWriter are not avaliable there in the API.How can I run the above example?
Payal sharma
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
To know more about this just visit http://jakarta.apache.org/taglibs/doc/xtags-doc/intro.html.
From there download xtags library..In that zip file u could find
xtags-examples.war file.Again if u extract this war file then u will find all necessary examples for your application.In that u could find dom4j.jar file too.Inside this jar file SAXReader and XMLwriter are found.
For the documents of SAXReader and XMLWriter visit http://dom4j.org/cookbook/cookbook.html
best regards,
paramaguru
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Parama guru,
Links provided by you are really informative. I could write and read a xml file very easily with this package.
Payal sharma
 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paramaguru
thanks for u'r reply.It was very useful.
Now i have a problem of how to wrote a in xml file thats is in the server.
FileOutputStream output = new FileOutputStream(E://sample.xml");
OutputFormat format = new OutputFormat(document);
XMLSerializer serializer = new XMLSerializer(output, format);
serializer.serialize(root);
output.flush();
output.close();
This is how i write into file in the local host ,But i need to write an xml file placed in the server.I have to give the xml file to be written as "http://sample.xml".
How could i do this.
pl.. help me.
with regards,
Sonara.
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry.I haven't tried this so far.I can give you the oral idea to do this.But i don't know whether it is suitable for your application.
Place this jsp/servlet file in the server where the xml file resides and try to open the file in write mode like this

FileOutputStream fos=new FileOutputStream("../sample.xml");
Now your updated contents can be written into the xml file which resides in the server..
Pardon me ,if this idea is helpless.
Best Regards,
Paramaguru.
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sonara,
If you are using servlets you can use
getServletContext().getRealPath("/WEB-INF/"+"student.xml");
if you are using jsp use
application.getRealPath("/WEB-INF/student.xml");

payal
 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
This were i find difficulty in writing with the xml file.

url=new URL("http://server/sample.xml");
URLConnection connection = url.openConnection();
connection.setAllowUserInteraction(true);//2 way communication
InputStream stream = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
String strCheckName=StartApplet.fileChooser.getSelectedFile().getPath();
if(!(strCheckName.toLowerCase().endsWith(strExt)))
strCheckName=strCheckName+strExt;
FileOutputStream file = new FileOutputStream(strCheckName);
The url specified in this way without servlets or jsp as told by u,works only for reading ,but it doesn't write in to the xml file.
Can u please help out to get the absolutepath specified by that url(http://).
Excepting u'r replies,
Sonara.
 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai friends,
Regarding the xml file.In an "existing" xml file i am able to update the existing element but i am not able to add a new element in the xml file.
To add new element i can only again create all the existing elements plus the new one.
So what can i do to add an element without creating all the existing elements plus the new one.
Pl... clarify my doubts.
with regards,
Sonara.
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Pls.find the reply posted by me in this same topic dated sep 14 2001.
Hope that would help.
Best Regards,
Paramaguru
 
reply
    Bookmark Topic Watch Topic
  • New Topic