If your documents will be small enough to fit in memory, then something like this in JDOM would work:
Element firmName = new Element("Firmname").setText("Gmbh & Co.KG");
A more efficient but (much) more complex way to create XML is to implement the SAX2 XMLReader interface; you can pass objects that implement this to most things that need XML input. eg:
// Warning - this is a sketch, writing an XMLReader is complicated.
// see [1] for superclass, which provides some more info -
// please read comments on that page, theres a bug in the download.
public class CompanyReader extends XMLReaderBase {
// most methods omitted, as are exceptions etc for clarity
public void parse(InputSource input) {
// this handling is inadequate - read [2]
// to see how InputSources should be dealt with
Reader reader = input.getCharacterStream();
contentHandler.startDocument();
// should obey SAX NS properties here!!
// EMPTY_ATTRIBUTES is just an empty Attributes object.
contentHandler.startElement(null, "Firmname", "Firmname", EMPTY_ATTRIBUTES);
char[] buffer = new char[1024];
int nread = 0;
while ((nread = reader.read(buffer)) > 0) {
// note this buffer contains 'unescaped' chars
// they are only escaped when the XML is
// serialized to a file using eg XMLWriter
contentHandler.characters(buffer, 0, nread);
}
contentHandler.endElement(null, "Firmname", "Firmname");
contentHandler.endDocument();
}
}
I guess in your application the saved data is 'supposed' to be XML? Not just a fragment you later incorporate into an XML document? Probably the best thing for you would be to save the submitted data as compliant XML using the JDOM method instead.
Hope this helps,
Baz
[1] http://sourceforge.net/tracker/index.php?func=detail&aid=492436&group_id=29449&atid=396222
[2]
http://java.sun.com/j2se/1.4/docs/api/org/xml/sax/InputSource.html
Steve Loughran wrote:
there is no CDATA in xml schema. irritating but true. so no CDATA in soap. base-64 encode or do the escaping. Somwhere in Axis (I forget where), the code to do the escaping exists.----- Original Message ----- From: "Akacem Mohammed" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, January 08, 2003 00:02 Subject: AW: & within a string / parser expects an entity reference Hello James, the information is being in the first place from a html form gathered and saved in a file. so I know there would be no entity reference in the xml file and it would be easier if it is possible to tell the parser not to expect an entity reference otherweise each input in the form musst be filtered. (something similar to #CDATA in DTD's). thanks for any hint Mohammed
