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");
        root.addContent(child);

        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);
            outputFormat.setIndent(2);

            SerializerFactory factory =
SerializerFactory.getSerializerFactory(Method.XML);
            Serializer serializer = factory.makeSerializer(out,
outputFormat);

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

            ContentHandler handler = serializer.asContentHandler();
            handler.startDocument();

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

            handler.endDocument();
        }
        catch (Exception e) {
            e.printStackTrace();
            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>
</patient>

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
so.

  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.

  Regards

  Pankaj Malhan



  >From: "???"
  >Reply-To: general@xml.apache.org
  >To:
  >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:
general-help@xml.apache.org

Mime
View raw message