File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Entity problem with JAXP/DOM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Entity problem with JAXP/DOM" Watch "Entity problem with JAXP/DOM" New topic
Author

Entity problem with JAXP/DOM

Zachary Kay
Greenhorn

Joined: Feb 11, 2003
Posts: 1
Hi.
I'm new to working with JAXP and DOM and I am having the following problem. Does anyone have any idea what I'm doing wrong?
My program builds an XML document in DOM and then dumps it to a file. I need to be able to support special characters in attribute values. Unfortunately, anytime I put out an entity or for that matter the numerical representation of a character, it gets altered. The "&" starting the entity is escaped to "&".
For example, If I want to have:
<product name="polyamplamine®istered_trade_mark;"/>
as my output.
I end up with
<product name="polyamplamine&registered_trade_mark;"/>
instead. Note the "&" to "&" conversion, i.e. the problem.
This is the code for creating the document.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
DocumentType dcp_companies_DOCTYPE = impl.createDocumentType("dcp",
"","file:///export/home/me/company.dtd");
doc = impl.createDocument(null,"dcp",dcp_companies_DOCTYPE);
I also tried it without the DTD. I believe it was:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
doc = impl.createDocument(null,"dcp",null);
When I go to create the product:
Element product = doc.createElement("product");
product.setAttribute("name",name); //where name is a string
When I serialize the DOM:
File file_obj = new File(output_file); //where output_file is a string
FileWriter fw = new FileWriter(file_obj);
// Serialize the document
OutputFormat format = new OutputFormat(doc);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(3);
XMLSerializer serializer = new XMLSerializer(fw, format);
serializer.serialize(doc);
fw.close();

Anybody have any suggestions or know of other non-JavaRanch listservers/groups for this topic?
Thanks a lot. I appreciate the help.
--Zachary--
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
How are you defining your entity in the XML?
- madhav


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
wood burning stoves
 
subject: Entity problem with JAXP/DOM