From Bob.Steemson@northgate-is.com Thu Feb 1 10:02:37 2001 Return-Path: Mailing-List: contact general-help@xml.apache.org; run by ezmlm Delivered-To: mailing list general@xml.apache.org Received: (qmail 56596 invoked from network); 1 Feb 2001 10:02:37 -0000 Received: from mailsweep1.northgate-is.com (152.114.1.101) by h31.sny.collab.net with SMTP; 1 Feb 2001 10:02:37 -0000 Received: from uk-heme-exc0.northgate-is.com (uk-heme-exc0.northgate-is.com) by mailsweep1.northgate-is.com (Content Technologies SMTPRS 4.1.5) with ESMTP id for ; Thu, 1 Feb 2001 10:02:22 +0000 Received: from bsteemson1 (bsteemson1.northgate-is.com [152.114.211.73]) by uk-heme-exc0.northgate-is.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2650.21) id WP5XHQKT; Thu, 1 Feb 2001 10:02:15 -0000 From: "Bob Steemson" To: Subject: RE: Inserting Data Using SAX Date: Thu, 1 Feb 2001 10:03:27 -0000 Message-ID: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0000_01C08C36.3894EAA0" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 In-Reply-To: X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N ------=_NextPart_000_0000_01C08C36.3894EAA0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit 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 ... "999 999 999" 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 ------=_NextPart_000_0000_01C08C36.3894EAA0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
I can=20 suggest two ways that I have used. You can build an object = model using=20 JDOM, which is pretty easy, and then send it to an=20 XMLOutputter ...
 
        = OutputStream=20 out;
        // = Initialise out here...
 
        = Element root=20 =3D new = Element("patient");
       =20 root.addAttribute("CRN", "123456");
 
       =20 Element child;
 
        = child =3D new=20 Element("nhs_number");
       =20 child.addContent("999 999 = 999");
       =20 root.addContent(child);
 
        = Document=20 document =3D new Document(root);
 
        = try=20 {
           =20 XMLOutputter outputter =3D new XMLOutputter("  ",=20 true);
          &nb= sp;=20 outputter.output(document, = out);
       =20 }
        catch = (java.io.IOException e)=20 {
            = throw=20 new = EHRException(e.getMessage());
      &nbs= p;=20 }
 
Alternatively, you can use the Xerces Serializer class. This = can give you=20 a ContentHandler reference. You generate the SAX2 events and it = translates=20 these into an XML document ...
 
        OutputStream=20 out;
 
        try=20 {
           =20 OutputFormat outputFormat =3D new OutputFormat(Method.XML,=20 OutputFormat.Defaults.Encoding,=20 true);
          &nb= sp;=20 outputFormat.setIndent(2);
 
          &nbs= p;=20 SerializerFactory factory =3D=20 SerializerFactory.getSerializerFactory(Method.XML);
   =         =20 Serializer serializer =3D factory.makeSerializer(out,=20 outputFormat);
 
          &nbs= p;=20 AttributesImpl attrs =3D new=20 AttributesImpl();
        &nbs= p;  =20 attrs.addAttribute("", "", "CRN", "CDATA", "123456" = );
          &nbs= p;=20 Attributes attributes =3D attrs;
          &nbs= p;=20 Attributes noAttributes =3D new AttributesImpl();

          =  =20 ContentHandler handler =3D=20 serializer.asContentHandler();
      &nb= sp;    =20 handler.startDocument();
 
          &nbs= p;=20 handler.startElement("", "patient", "", attributes);
          &nbs= p;=20 handler.startElement("", "nhs_number", "", = noAttributes);
          &nbs= p;=20 handler.characters("999 999 999".toCharArray(), 0, = 11);
          &nbs= p;=20 handler.endElement("", "nhs_number");
          &nbs= p;=20 handler.endElement("", "patient");
 
          &nbs= p;=20 handler.endDocument();
       =20 }
        catch (Exception e)=20 {
           =20 e.printStackTrace();
        &= nbsp;  =20 throw new=20 EHRException(e.getMessage());
      &nbs= p;=20 }
Both=20 code extracts should build something like ...
 
<?xml version=3D1.0" ?>
<patient CRN+"123456">
    <nhs_number>"999 999=20 999"</nhs_number>
</patient>
 
There=20 are lots of options for formatting, etc - see the relevent=20 APIs.
 
Apologies if there are bugs in the code. It has been cut = from much=20 longer routines and chopped around to remove complications like = namespaces, so=20 it probably won't work straight out of the box! Hope it helps=20 anyway :-)
          &nbs= p;=20
 
Bob=20 Steemson
Principal Systems Architect
PDS Health Group
Northgate Information=20 Solutions

The opinions herein are = my own and,=20 unless explicitly stated, may not represent those of Northgate = Information=20 Solutions UK Limited, part of Northgate Information Solutions = plc.

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

hi there!!

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

Is that so we need any other helper classes or interface = to intsert=20 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=20
>To:
>Subject: Inserting Data Using SAX=20
>Date: Wed, 31 Jan 2001 10:17:14 +0900=20
>=20
>Hello, there.=20
>=20
>I have a question about SAX.=20
>=20
>How can I insert a new element into the XML file, or = delete=20 elements, using SAX?=20
>=20
>SAX defines events method, startDocument(), = endDocument()....,=20 but does not define createElement(), createComment()... like DOM.=20
>=20
>So, please tell me how I can solve my problem.=20
>=20
>Thanx in advance.=20


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

------------------------------------------------------------------= ---=20 In case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, = e-mail:=20 general-unsubscribe@xml.apache.org For additional commands, e-mail:=20 general-help@xml.apache.org
------=_NextPart_000_0000_01C08C36.3894EAA0--