xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "pankaj malhan" <pankajmal...@hotmail.com>
Subject Manipulating a Xml Document.??
Date Mon, 29 Jan 2001 18:18:13 GMT
<html><DIV>
<P><BR>hi <BR>i'm new to XML.<BR>i want to maipulate a XML document
(ADD attribute or<BR>delete or add a new element into existing XML document)<BR>i'm
able to to all this by using DOm (I was using Ibm's parser)<BR>now i want to manifulate
the XML document using <BR>SAX2.0&nbsp; .Can i do that ???.<BR>i'm able to
parse and read the XML document by using ContentHandler <BR>Interface but i dont know
how to manipulate the XML document.<BR>I'm using Xalan for Transformations of XML to
HTML and WML.<BR>Is that i have to use Xerces???<BR>i have also dowmloaded JDOM
API.How to Procced??....??</P>
<P>Thx.</P>
<P>Pankaj Malhan</P>
<P>here is a sample code to manipulate a XML Document and the XML document.&nbsp;</P>
<P><BR>import java.sql.*;<BR>import java.util.*;<BR>import java.io.*;</P>
<P>import com.ibm.xml.parser.*;<BR>import org.w3c.dom.DOMException;<BR>import
org.w3c.dom.*;&nbsp;&nbsp; <BR>public class malhan1 <BR>{<BR>.....<BR>&nbsp;
....<BR>&nbsp;&nbsp;&nbsp; ......<BR>&nbsp;&nbsp; Parser p1=new
Parser("mama");<BR>&nbsp;&nbsp;&nbsp; ByteArrayInputStream bais=new ByteArrayInputStream(prvxmlstring.getBytes());<BR>&nbsp;&nbsp;&nbsp;
TXDocument doc = null;<BR>&nbsp;&nbsp;&nbsp; doc=p1.readStream(bais);<BR>&nbsp;&nbsp;&nbsp;
....<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ....<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
...</P>
<P><BR>if(doc!=null)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
String root = doc.getRootName();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
System.out.println("Root name&nbsp; "+root);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if(root!=null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(root.equalsIgnoreCase("SALESMAN_5"))<BR>&nbsp;&nbsp;&nbsp;
{<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
for(Node Child=doc.getDocumentElement().getFirstChild();Child!=null;Child=Child.getNextSibling())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if(Child instanceof Element)&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String name=Child.getNodeName();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(name);&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(Child.getNodeName().equals("OPPORTUNITYS"))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//item.appendChild(doc.createTextNode("OPPORTUNITY_ID=\"100\"
NAME=\"xyz\" DESCRIPTION=\"PANKAJ MALHAN\"&nbsp; DEPARTMENT=\"abc\" CONTACT_ID=\"3\" OPENING_DATE=\"12/12/2000\"&nbsp;
CLOSING_DATE=\"12/31/2000\" REVENUE=\"1000\" PROBABILITY=\"10\" COMPETITORS=\"abc,xyz\""));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//item.appendChild(doc.createTextNode("NAME"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element
item=doc.createElement("OPPORTUNITY");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.setAttribute("ID","4");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.setAttribute("NAME","malhan"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.setAttribute("CID","2");&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Element
item2=doc.createElement("DESCRIPTION");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2.appendChild(doc.createTextNode("good
boy"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(item2);<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2=doc.createElement("DEPARTMENT");<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2.appendChild(doc.createTextNode("sss"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(item2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2=doc.createElement("CLOSING_DATE");<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2.appendChild(doc.createTextNode("23-dec-1977"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(item2);&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2=doc.createElement("REVENUE");<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2.appendChild(doc.createTextNode("23889389"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(item2);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2=doc.createElement("PROBABILITY");<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2.appendChild(doc.createTextNode("23889389"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(item2);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2=doc.createElement("COMPETITORS");<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item2.appendChild(doc.createTextNode("abc,
xyz,wdj"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.appendChild(item2);<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Child.appendChild(item);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</P>
<P>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;FileOutputStream
fo=new FileOutputStream("personal_details_5.xml");<BR>&nbsp;&nbsp;&nbsp;((TXDocument)doc).setVersion("1.0");<BR>&nbsp;&nbsp;&nbsp;((TXDocument)doc).printWithFormat(new
PrintWriter(fo));&nbsp;<BR>&nbsp;&nbsp;&nbsp;fo.close();&nbsp;&nbsp;&nbsp;</P>
<P>&nbsp;&nbsp; }&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;
}<BR></P>
<P>-------------------------XML File----------------</P>
<P>&lt;?xml version="1.0"?&gt;<BR>&lt;SALESMAN_4&gt;<BR>&nbsp;
&lt;SALESDET NAME="pankaj" TITLE="consultant" ACCOUNT_ID="1" PHONE="5447737" ACCOUNT_NAME="ost"
FAX="98439" ADDRESS="orgok" EMAIL="<A href="mailto:pankajmalhan@yahoo.com">pankajmalhan@yahoo.com</A>"
CITY="noida" TERRITORY="noida" STATE="delhi" ZIP="110063" COUNTRY_ID="97" WEBSITE="pankaj.com"
CURRENCY_ID="9"&gt; <BR>&nbsp;&nbsp; &lt;/SALESDET&gt;<BR>&nbsp;
&lt;OPPORTUNITYS&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;OPPORTUNITY&gt;<BR>&lt;OPPORTUNITY_ID="100"
NAME="xyz" DESCRIPTION="PANKAJ MALHAN"&nbsp; DEPARTMENT="abc" CONTACT_ID="3" OPENING_DATE="12/12/2000"&nbsp;
CLOSING_DATE="12/31/2000" REVENUE="1000" PROBABILITY="10" COMPETITORS="abc,xyz"/&gt;<BR>&nbsp;&lt;/OPPORTUNITY&gt;
<BR>&nbsp;&lt;/OPPORTUNITYS&gt;<BR>&lt;/SALESMAN_4&gt;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
------------------------------</P>
<P>&nbsp;</P></DIV><br clear=all><hr>Get Your Private, Free
E-mail from MSN Hotmail at <a href="http://www.hotmail.com">http://www.hotmail.com</a>.<br></p></html>

Mime
View raw message