• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity problem with JAXP/DOM

 
Zachary Kay
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you defining your entity in the XML?
- madhav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic