You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Gude Reshma <re...@yahoo.com> on 2002/08/09 16:19:12 UTC
Bug in setAttribute???
hi .
i am using DOM to create an XML file .I am using methods like setAttribute to set attribute of the created element. But sometmies i find that the attribute is not set properly. for example if i have an element named 'e' & i do e.setAttribute('i', "value"); sometimes i find that i ge tsomething like <e i="></e>.
I am using XML serializer to convert the element i get into XML. So when i parse the XML i get an expcetion bcoz tis not well formed.
can anyone help??
---------------------------------
Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs
Re: Bug in setAttribute???
Posted by Gude Reshma <re...@yahoo.com>.
No i'm not using threads. Here is my sample code:
import org.w3c.dom.*;
import java.io.*;
import org.apache.xerces.parsers.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.apache.xml.serialize.*;
public class Test1
{
public static void main(String args[])
{
Test1 t1 = new Test1();
String s = "<parent id='1'></parent>";
Document doc = t1.parseString(s);
Element parent_element = doc.getDocumentElement();
Element child = doc.createElement("child");
child.setAttribute("id","1");
parent_element.appendChild(child);
s = t1.getDocumentAsString(doc);
//this is sometimes wrong,i.e i get s as <parent id='1'><child id="></child></parent>
System.out.println(s);
}
public Document parseString(String str)
{
Document doc = null;
try
{
byte[] byte_array_of_string = str.getBytes();
InputStream istr = new ByteArrayInputStream(byte_array_of_string);
InputSource is = new InputSource(istr);
DOMParser dom_parser = new DOMParser();
dom_parser.parse(is);
doc = dom_parser.getDocument();
}
catch(Exception e)
{
System.out.println("Error in parseString :: "+e);
}
return(doc);
}
public String getDocumentAsString(Document doc)
{
ByteArrayOutputStream str = new ByteArrayOutputStream();
try
{
long l = System.currentTimeMillis();
OutputFormat format = new OutputFormat("XML","UTF-8",true);
XMLSerializer serializer = new XMLSerializer(str, format);
serializer.serialize(doc.getDocumentElement());
long l1 = System.currentTimeMillis();
}
catch(Exception e)
{
System.out.println("Exception in getDocumentAsString "+e);
}
String s = str.toString();
return s;
}
}
Elena Litani wrote:Gude Reshma wrote:
> i am using DOM to create an XML file .I am using methods like
> setAttribute to set attribute of the created element. But sometmies i
> find that the attribute is not set properly. for example if i have an
> element named 'e' & i do e.setAttribute('i', "value"); sometimes i
> find that i ge tsomething like
---------------------------------
Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs
Re: Bug in setAttribute???
Posted by Elena Litani <el...@ca.ibm.com>.
Gude Reshma wrote:
> i am using DOM to create an XML file .I am using methods like
> setAttribute to set attribute of the created element. But sometmies i
> find that the attribute is not set properly. for example if i have an
> element named 'e' & i do e.setAttribute('i', "value"); sometimes i
> find that i ge tsomething like <e i="></e>.
This is really weird -- do you use threads in your application?
If yes, you need to synchronize access the DOM tree, since our
implementation is not thread safe.
If not, please, provide a sample code that shows the bug.
Thanks!
--
Elena Litani / IBM Toronto
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org