xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bob Steemson" <Bob.Steem...@northgate-is.com>
Subject RE: Inserting Data Using SAX
Date Thu, 01 Feb 2001 10:03:27 GMT
I can suggest two ways that I have used. You can build an object model using
JDOM, which is pretty easy, and then send it to an XMLOutputter ...

        OutputStream out;
        // Initialise out here...

        Element root = new Element("patient");
        root.addAttribute("CRN", "123456");

        Element child;

        child = new Element("nhs_number");
        child.addContent("999 999 999");

        Document document = new Document(root);

        try {
            XMLOutputter outputter = new XMLOutputter("  ", true);
            outputter.output(document, out);
        catch (java.io.IOException e) {
            throw new EHRException(e.getMessage());

Alternatively, you can use the Xerces Serializer class. This can give you a
ContentHandler reference. You generate the SAX2 events and it translates
these into an XML document ...

        OutputStream out;

        try {
            OutputFormat outputFormat = new OutputFormat(Method.XML,
OutputFormat.Defaults.Encoding, true);

            SerializerFactory factory =
            Serializer serializer = factory.makeSerializer(out,

            AttributesImpl attrs = new AttributesImpl();
            attrs.addAttribute("", "", "CRN", "CDATA", "123456" );
            Attributes attributes = attrs;
            Attributes noAttributes = new AttributesImpl();

            ContentHandler handler = serializer.asContentHandler();

            handler.startElement("", "patient", "", attributes);
            handler.startElement("", "nhs_number", "", noAttributes);
            handler.characters("999 999 999".toCharArray(), 0, 11);
            handler.endElement("", "nhs_number");
            handler.endElement("", "patient");

        catch (Exception e) {
            throw new EHRException(e.getMessage());

Both code extracts should build something like ...

<?xml version=1.0" ?>
<patient CRN+"123456">
    <nhs_number>"999 999 999"</nhs_number>

There are lots of options for formatting, etc - see the relevent APIs.

Apologies if there are bugs in the code. It has been cut from much longer
routines and chopped around to remove complications like namespaces, so it
probably won't work straight out of the box! Hope it helps anyway :-)

Bob Steemson
Principal Systems Architect
PDS Health Group
Northgate Information Solutions
The opinions herein are my own and, unless explicitly stated, may not
represent those of Northgate Information Solutions UK Limited, part of
Northgate Information Solutions plc.

  -----Original Message-----
  From: pankaj malhan [mailto:pankajmalhan@hotmail.com]
  Sent: 31 January 2001 18:49
  To: general@xml.apache.org
  Subject: Re: Inserting Data Using SAX

  hi there!!

  i'm aslo facing simmilar problem !i'm manupulating the XML file using DOM
but i can only find inteface like content handler insax2 to parse and read
the xml file . ilooked at API docs of Sax2  but couldnt find methods to do

  Is that so we need any other helper classes or interface to intsert data
in xml file???

  If anyone could be precise it would be a great help.


  Pankaj Malhan

  >From: "???"
  >Reply-To: general@xml.apache.org
  >Subject: Inserting Data Using SAX
  >Date: Wed, 31 Jan 2001 10:17:14 +0900
  >Hello, there.
  >I have a question about SAX.
  >How can I insert a new element into the XML file, or delete elements,
using SAX?
  >SAX defines events method, startDocument(), endDocument()...., but does
not define createElement(), createComment()... like DOM.
  >So, please tell me how I can solve my problem.
  >Thanx in advance.

  Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

  --------------------------------------------------------------------- In
case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, e-mail:
general-unsubscribe@xml.apache.org For additional commands, e-mail:

View raw message