The Artima Developer Community
Sponsored Link

Java Answers Forum
how to read write update a xml file.

3 replies on 1 page. Most recent reply: Mar 17, 2002 5:40 PM by xmlGUY

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
unimon

Posts: 1
Nickname: promethe
Registered: Mar, 2002

how to read write update a xml file. Posted: Mar 7, 2002 2:27 AM
Reply to this message Reply
Advertisement
first i am french so i do not speak english very well
so thanks you for read me.

then i an begginer in java and xml.

my question.

the file to access is this one: agenda.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<PAGE>
<AGENDA>
<TITRE>titre</TITRE>
<DATE>date</DATE>
<TEXTE>texte</TEXTE>
</AGENDA>
</PAGE>

using java and DOM, i am trying update de file like this.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<PAGE>
<AGENDA>
<TITRE>titre0</TITRE>
<DATE>date0</DATE>
<TEXTE>texte0</TEXTE>
</AGENDA>

<AGENDA>
<TITRE>titre1</TITRE>
<DATE>date1</DATE>
<TEXTE>texte1</TEXTE>
</AGENDA>
</PAGE>

i have no idea of how to do

thanks you
Merci d'avance


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: how to read write update a xml file. Posted: Mar 7, 2002 9:52 AM
Reply to this message Reply
You may wish to use JDOM instead of DOM. JDOM is a much simpler API, because DOM is a language-independent API for parsing and manipulating XML documents. As a result, it's clunky in Java -- quite complex, actually. JDOM does much the same stuff, but without trying to be true to a language-independent pre-defined API. JDOM is here:

http://www.jdom.org

You may also want to check out JAXB. I use both JDOM and JAXB for my whiz-bang web site/book build system. (In fact, JAXB played a role in generating this very page you are looking at.) I prefer JAXB for small, well-known DTDs, and use JDOM when I have a big huge not-so- well-understood data structure. With JAXB, you actually give your DTD to a program that generates classes for you that map to the data structure. So if you have a page element, you end up getting a Page class, which serves as both a parser and holds the resulting DOM. Find out about JAXB here:

http://java.sun.com/xml/jaxb/

Anil S.

Posts: 13
Nickname: anil
Registered: Feb, 2002

Re: how to read write update a xml file. Posted: Mar 7, 2002 2:51 PM
Reply to this message Reply
Bonjour!

you might wanna look into parsing your XML using
a DOM based parser, can be downloaded from the
apache.org/alphaworks.ibm.com site, using it you
can read and update your XML, study the examples, JDOM
as Bill mentioned will simplify your programming
efforts if you're comfortable with Java.

you'll find lotta reading resources on Java/XML over
the internet, google your Java/XML queries on
google.com

checkout IBM's repository for Java/XML 'n more:
alphaworks.ibm.com

au revoir for now!
d'anil

> first i am french so i do not speak english very
> well so thanks you for read me.

> then i an begginer in java and xml.
>
> my question.
>
> the file to access is this one: agenda.xml
>
> <?xml version="1.0" encoding="ISO-8859-1" ?>
> <PAGE>
> <AGENDA>
> <TITRE>titre</TITRE>
> <DATE>date</DATE>
> <TEXTE>texte</TEXTE>
> </AGENDA>
> </PAGE>
>
> using java and DOM, i am trying update de file like
> this.
>
> <?xml version="1.0" encoding="ISO-8859-1" ?>
> <PAGE>
> <AGENDA>
> <TITRE>titre0</TITRE>
> <DATE>date0</DATE>
> <TEXTE>texte0</TEXTE>
> </AGENDA>
>
> <AGENDA>
> <TITRE>titre1</TITRE>
> <DATE>date1</DATE>
> <TEXTE>texte1</TEXTE>
> </AGENDA>
> </PAGE>
>
> i have no idea of how to do
>
> thanks you
> Merci d'avance

xmlGUY

Posts: 1
Nickname: xmlguy
Registered: Mar, 2002

Re: how to read write update a xml file. Posted: Mar 17, 2002 5:40 PM
Reply to this message Reply
Well I didn't feel much like doing more than this for you.. sorry. But this gives you a great start as far as I'm concerned....I can;t remember how many methods are in this, but some of them seem to be complete junk, yet they work. So, for those of you out there snickering at my code.. this is not in any production environment. So........



import java.io.*;
import java.util.*;
import java.lang.Exception;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DocumentType;
import javax.xml.parsers.*;




public class xmlRPT
{


int NoChild=0;

FileWriter out;
File xmlOut;

Node rootElement = null;
Element agenda = null;
Element date = null;
Element title=null;
Element text = null;
Document doc= null;

public void setValues()


{
try {



xmlOut= new File("agenda.xml");



if (xmlOut.exists()==false){

//if the document doesnt exist we will write all the header stuff.. and the root tag."PAGE"

out = new FileWriter("report.xml",true);
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
out.write("<REPORT>");

//then we will start writing the agenda tag and child elements.
printDOMTree(agenda, out);


}
else{
//create a new filewriter to write to your XML file
out = new FileWriter("report.xml",true);

//we will print the elements in memory starting with Agenda. not with Page.
printDOMTree(agenda ,out);




}
xmlOut = null;
out.flush();
out.close();
out=null;
System.gc();
}catch(IOException ioe) {
System.out.println(ioe);
}
catch(Exception e){
System.out.println(e);


}




}




public void prepRpt(int onoff){
try {
out = new FileWriter("report.xml",true);


if (onoff==0){

out = new FileWriter("report.xml",true);
out.write("\n</AGENDA>");
out.flush();
out.close();
out = null;
}
else {
if (onoff==1){

RandomAccessFile raf = new RandomAccessFile("agenda.xml", "rw");
long lenFile =raf.getFilePointer();
//this next line will start writing 9 bytes before the end of the line..
long putFile = lenFile + raf.length() - 9;

raf.seek(putFile);
raf.writeUTF(" ");

raf.close();

raf=null;
}
}
System.gc();

}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(Exception e){
System.out.println(e);


}


}

public void parseOut(String values[])
{

try
{

DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
doc = impl.createDocument(
"", "PAGE", null);
}
catch (ParserConfigurationException cnfe)
{
System.out.println(cnfe);
}


if (doc != null)
{
// Find the implementation


// Create the document

// Fill the document
rootElement = doc.getDocumentElement();
agenda = doc.createElement("AGENDA");

title = doc.createElement("TITLE");
title.appendChild(doc.createTextNode("TITLE HERE"));
agenda.appendChild(title);


date = doc.createElement("DATE");
date.appendChild(doc.createTextNode("DATE HERE"));
agenda.appendChild(date);

text = doc.createElement("TEXT");
text.appendChild(doc.createTextNode("TEXT HERE"));
agenda.appendChild(text);





rootElement.appendChild(agenda);



}
}

/** Prints the specified node, recursively. */
public void printDOMTree(Node node, FileWriter out) throws IOException

{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
printDOMTree(((Document)node).getDocumentElement(), out);
break;
}

// print element with attributes
case Node.ELEMENT_NODE:
{
if (node.getNodeName()=="EMPLOYEE"){
out.write("\n<");}
else{
out.write("\n <");
}
out.write(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
out.write(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
}
out.write(">");

NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++){

printDOMTree(children.item(i), out);}
}

break;
}

// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
out.write("&");
out.write(node.getNodeName());
out.write(";");
break;
}

// print cdata sections
case Node.CDATA_SECTION_NODE:
{
out.write("<![CDATA[");
out.write(node.getNodeValue());
out.write("]]>");
break;
}

// print text
case Node.TEXT_NODE:
{
out.write(node.getNodeValue());
break;
}

// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
out.write("<?");
out.write(node.getNodeName());
String data = node.getNodeValue();
{
out.write(" ");
out.write(data);
}
out.write("?>");
break;
}
}

if (type == Node.ELEMENT_NODE)
{
if (node.getNodeName()=="EMPLOYEE"){
out.write("\n");
}
out.write("</");
out.write(node.getNodeName());
out.write('>');
}
} // printDOMTree(Node, PrintWriter)




}

Flat View: This topic has 3 replies on 1 page
Topic: java paths in linux? Previous Topic   Next Topic Topic: reading text file through applet - secruity exception

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use