You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Javier Gálvez Guerrero <du...@gmail.com> on 2007/11/13 23:29:42 UTC
Writing to an existing XML file
Hi there,
I would like to know how I can write to an existing XML file. I have just
discovered Xerces and I've tried to find the answer by myself through the
API on the web but I can not find it.
Can anyone help me, please?
Thanks a lot,
Javi
Re: Writing to an existing XML file
Posted by Axel Weiss <aw...@informatik.hu-berlin.de>.
On Sat, 2007-11-17 at 19:53 +0100, Javier Gálvez Guerrero wrote:
> Any guide points, references, links or examples would be so much
> appreciated.
Javier,
the easy way to begin with xerces is to study the examples in
xerces-c-src_2_8_0/samples. Maybe you want to begin with the DOM parser,
since you need less to do. Also,
http://xerces.apache.org/xerces-c/apiDocs/classes.html has always been a
good reference to me.
> ... That means how can I change the DOM tree as been said before? And
> how to serialize it?
You can create a DOMDocument in different ways. If you want to read an
existing file, you need a DOMBuilder object, e.g. created by
DOMImplementation::createDOMBuilder or
DOMImplementation::createLSParser. Then you can read (and parse) a file
using DOMBuilder::parseURI.
DOMDocument::getDocumentElement gives you the root element, representing
the root element in the file. You can then navigate and manipulate the
tree with the methods of DOMElement and DOMNode.
For serialization, you may want to 'pretty print' the tree with some
w/s-nodes and give it to a DOMWriter object, i.e. method writeToString.
> On the other side I know that using SAX I could override startElement
> and endElement
> methods in order to define what I want to do in the XML file parsing process
> but I don't know if this applies to DOM as well or (if not) which methods I
> must override or use.
DOM and SAX are 'different worlds'. I would not mix them up. DOM is best
for (small) xml files, SAX is better in streams or huge files.
HTH,
Axel
Re: Writing to an existing XML file
Posted by Javier Gálvez Guerrero <du...@gmail.com>.
Thanks Alex, your help is much appreciated.
However and as you may already know..I am a completely newbie in Xerces. So
I have the big picture about what I have to do but I don't know how (class
and methods' names and use). How can I create a new element to be written in
a parsed XML file with the DOMWriter? With the writeNode method? How can I
specify the file, the type of node (element or attribute) and the content
itself? That means how can I change the DOM tree as been said before? And
how to serialize it?
On the other side I know that using SAX I could override startElement
and endElement
methods in order to define what I want to do in the XML file parsing process
but I don't know if this applies to DOM as well or (if not) which methods I
must override or use.
Sorry for the inconvenience created by my questions but I am trying to find
the path to start with Xerces after searching for classes and methods in the
api and finding nothing clear enough (probably because of my lack of
knowledge on Xerces and these kind of tools).
Any guide points, references, links or examples would be so much
appreciated.
Sorry again and thank you very much,
Javi
2007/11/16, Axel Weiss <aw...@informatik.hu-berlin.de>:
>
> On Thu, 2007-11-15 at 07:58 +0100, Javier Gálvez Guerrero wrote:
> > Ok, sorry.
> >
> > I'm designing an IPTV client-server application that transports video on
> > demand. The server has to manage with user profiles and an electronic
> guide
> > of contents. There is a XML file for the guide and one for each user.
> >
> > In the server side, what I need to do is this. Each time the admin
> enters
> > through a GUI the new parameters of a content I would like to add this
> new
> > content on the guide xml file. "Content" is an element in the "Contents'
> > Guide" XML schema. Then I need to parse a C++ object (content) to a XML
> > element (content, in content_guide XML file). Moreover, each time a new
> user
> > signs in the service, the server should create a new XML file for the
> user,
> > based on the XML schema for a user.
>
> Javier:
> one way to edit xml files with xerces is
> - read the existing xml file into a DOM representation,
> - make your desired changes to the DOM tree,
> - serialize the DOM tree to a file (which can have the original
> filename).
>
> You can also create xml files (e.g. for your user) with the steps
> - create a DOM tree (e.g. DOM_Document),
> - populate it with some elements,
> - serialize it to a file.
>
> You cannot validate your changes against a schema/DTD while it is
> represented in DOM (which has never been intended by the standards). In
> order to validate a DOM tree, you need to serialize it (e.g. into a
> buffer) and re-parse.
>
>
> HTH,
> Axel
>
> > In conclusion, what I need is to know how can I parse from C++ objects
> or
> > attributes to an XML file in two cases: when I need to create a XML file
> > based on an existing XML schema (user) and add a new element (content)
> on an
> > existing XML file (content_guide). I'm about to use Xerces C++ 2.8.0 for
> > Windows but, in case this version didn't support the feature I need
> there
> > would be no problem to switch to any previous version. What I need is
> some
> > slight guiding points or whatever it could help (the API, a sample, a
> > link...) to notice how can I do it as I haven't been able to find it by
> > myself.
> >
> > I hope this helps. Thanks a lot,
> >
> > Javi
> >
> >
> >
> > 2007/11/15, David Bertoni <db...@apache.org>:
> > >
> > > Javier Gálvez Guerrero wrote:
> > > > I suppose you refer to the zip pack it can be downloaded from the
> > > Xerces'
> > > > site. I had already looked for any XML file writing sample and in
> the
> > > > website as well but I didn't find anything. Are you referring to any
> > > other
> > > > thing?
> > >
> > > Part of the problem is your question is too vague to answer properly.
> > > Xerces-C has functionality to serialize an instance of the DOM, but
> how
> > > it's done depends on what version of Xerces-C you're using.
> > >
> > > Please describe what you plan to do with Xerces-C, and someone will be
> > > able
> > > to help.
> > >
> > > Dave
> > >
>
>
Re: Writing to an existing XML file
Posted by Axel Weiss <aw...@informatik.hu-berlin.de>.
On Thu, 2007-11-15 at 07:58 +0100, Javier Gálvez Guerrero wrote:
> Ok, sorry.
>
> I'm designing an IPTV client-server application that transports video on
> demand. The server has to manage with user profiles and an electronic guide
> of contents. There is a XML file for the guide and one for each user.
>
> In the server side, what I need to do is this. Each time the admin enters
> through a GUI the new parameters of a content I would like to add this new
> content on the guide xml file. "Content" is an element in the "Contents'
> Guide" XML schema. Then I need to parse a C++ object (content) to a XML
> element (content, in content_guide XML file). Moreover, each time a new user
> signs in the service, the server should create a new XML file for the user,
> based on the XML schema for a user.
Javier:
one way to edit xml files with xerces is
- read the existing xml file into a DOM representation,
- make your desired changes to the DOM tree,
- serialize the DOM tree to a file (which can have the original
filename).
You can also create xml files (e.g. for your user) with the steps
- create a DOM tree (e.g. DOM_Document),
- populate it with some elements,
- serialize it to a file.
You cannot validate your changes against a schema/DTD while it is
represented in DOM (which has never been intended by the standards). In
order to validate a DOM tree, you need to serialize it (e.g. into a
buffer) and re-parse.
HTH,
Axel
> In conclusion, what I need is to know how can I parse from C++ objects or
> attributes to an XML file in two cases: when I need to create a XML file
> based on an existing XML schema (user) and add a new element (content) on an
> existing XML file (content_guide). I'm about to use Xerces C++ 2.8.0 for
> Windows but, in case this version didn't support the feature I need there
> would be no problem to switch to any previous version. What I need is some
> slight guiding points or whatever it could help (the API, a sample, a
> link...) to notice how can I do it as I haven't been able to find it by
> myself.
>
> I hope this helps. Thanks a lot,
>
> Javi
>
>
>
> 2007/11/15, David Bertoni <db...@apache.org>:
> >
> > Javier Gálvez Guerrero wrote:
> > > I suppose you refer to the zip pack it can be downloaded from the
> > Xerces'
> > > site. I had already looked for any XML file writing sample and in the
> > > website as well but I didn't find anything. Are you referring to any
> > other
> > > thing?
> >
> > Part of the problem is your question is too vague to answer properly.
> > Xerces-C has functionality to serialize an instance of the DOM, but how
> > it's done depends on what version of Xerces-C you're using.
> >
> > Please describe what you plan to do with Xerces-C, and someone will be
> > able
> > to help.
> >
> > Dave
> >
Re: Writing to an existing XML file
Posted by Javier Gálvez Guerrero <du...@gmail.com>.
Ok, sorry.
I'm designing an IPTV client-server application that transports video on
demand. The server has to manage with user profiles and an electronic guide
of contents. There is a XML file for the guide and one for each user.
In the server side, what I need to do is this. Each time the admin enters
through a GUI the new parameters of a content I would like to add this new
content on the guide xml file. "Content" is an element in the "Contents'
Guide" XML schema. Then I need to parse a C++ object (content) to a XML
element (content, in content_guide XML file). Moreover, each time a new user
signs in the service, the server should create a new XML file for the user,
based on the XML schema for a user.
In conclusion, what I need is to know how can I parse from C++ objects or
attributes to an XML file in two cases: when I need to create a XML file
based on an existing XML schema (user) and add a new element (content) on an
existing XML file (content_guide). I'm about to use Xerces C++ 2.8.0 for
Windows but, in case this version didn't support the feature I need there
would be no problem to switch to any previous version. What I need is some
slight guiding points or whatever it could help (the API, a sample, a
link...) to notice how can I do it as I haven't been able to find it by
myself.
I hope this helps. Thanks a lot,
Javi
2007/11/15, David Bertoni <db...@apache.org>:
>
> Javier Gálvez Guerrero wrote:
> > I suppose you refer to the zip pack it can be downloaded from the
> Xerces'
> > site. I had already looked for any XML file writing sample and in the
> > website as well but I didn't find anything. Are you referring to any
> other
> > thing?
>
> Part of the problem is your question is too vague to answer properly.
> Xerces-C has functionality to serialize an instance of the DOM, but how
> it's done depends on what version of Xerces-C you're using.
>
> Please describe what you plan to do with Xerces-C, and someone will be
> able
> to help.
>
> Dave
>
Re: Writing to an existing XML file
Posted by David Bertoni <db...@apache.org>.
Javier Gálvez Guerrero wrote:
> I suppose you refer to the zip pack it can be downloaded from the Xerces'
> site. I had already looked for any XML file writing sample and in the
> website as well but I didn't find anything. Are you referring to any other
> thing?
Part of the problem is your question is too vague to answer properly.
Xerces-C has functionality to serialize an instance of the DOM, but how
it's done depends on what version of Xerces-C you're using.
Please describe what you plan to do with Xerces-C, and someone will be able
to help.
Dave
Re: Writing to an existing XML file
Posted by Javier Gálvez Guerrero <du...@gmail.com>.
I suppose you refer to the zip pack it can be downloaded from the Xerces'
site. I had already looked for any XML file writing sample and in the
website as well but I didn't find anything. Are you referring to any other
thing?
Thanks a lot,
Javi
2007/11/14, barbapapaz <ba...@gmail.com>:
>
> Hi,
>
> In xerces pack you have folder sample, all that you need is into.
>
> 2007/11/13, Javier Gálvez Guerrero <du...@gmail.com>:
> > Hi there,
> >
> > I would like to know how I can write to an existing XML file. I have
> just
> > discovered Xerces and I've tried to find the answer by myself through
> the
> > API on the web but I can not find it.
> >
> > Can anyone help me, please?
> >
> >
> > Thanks a lot,
> >
> > Javi
> >
>
Re: Writing to an existing XML file
Posted by barbapapaz <ba...@gmail.com>.
Hi,
In xerces pack you have folder sample, all that you need is into.
2007/11/13, Javier Gálvez Guerrero <du...@gmail.com>:
> Hi there,
>
> I would like to know how I can write to an existing XML file. I have just
> discovered Xerces and I've tried to find the answer by myself through the
> API on the web but I can not find it.
>
> Can anyone help me, please?
>
>
> Thanks a lot,
>
> Javi
>