[xsd-users] how to insert/remove elements?
Gennady Khokhorin
gok at aerometric-ak.com
Fri Feb 15 22:00:11 EST 2008
Boris,
I went to the next studying level, compiled my own xml driver.
Can not figure out how to edit items in the tree.
I can add/edit/remove items in-memory and see its happened using
std::cout.
But storing tree to external file has no effect, its stored in original
state.
Classes was built with --generate-serialization option.
Input and output looks like:
<metadata>
<idinfo>
<citation>
<citeinfo>
<origin>can not change it</origin>
</citeinfo>
</citation>
</idinfo>
</metadata>
Code snippet:
std::auto_ptr<MetadataType> h (metadata (argv[1],
::xml_schema::Flags::dont_validate) );
IdinfoType idInfo = h->idinfo();
CitationType citation = idInfo.citation();
CiteinfoType citeInfo=citation.citeinfo();
CiteinfoType::OriginSequence
originator=citeInfo.origin();
originator.clear();
originator.push_back("gok");
for ( CiteinfoType::OriginIterator it =
originator.begin();
it != originator.end();
++it )
{
// control tree changes
std::cout << "originator:" << *it << std::endl;
}
// Serialize to a file.
std::ofstream ofs ("d:\\mine_o.xml");
metadata(ofs, *h, map, "UTF-8", 0);
Should the tree somehow be updated before streaming it out?
It is not a case for hello sample, all in-place changes I can see in
output file.
Please, give me an idea why its not acting as expected.
Regards,
Gennady
-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com]
Sent: Wednesday, February 13, 2008 11:01 AM
To: Gennady Khokhorin
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsde-users] how to insert/remove elements?
Hi Gennady,
In the future please keep your replies CC'ed to the xsd-users mailing
list. This way other developers who may have experienced a similar
problem can provide you with a solution. Plus questions and answers will
be archived and available to others with similar problems.
Gennady Khokhorin <gok at aerometric-ak.com> writes:
> One tiny question/wish: streaming out xml with std::ofstream created
> an extra carriage return symbol after each element:
> <?xml version="1.0" encoding="ISO8859-1" standalone="no" ?> <hello>
>
> <greeting>Hello</greeting>
>
> <name>its me</name>
>
> </hello>
>
> How to drop it?
XSD uses DOMWriter provided by Xerces-C++ to serialize to XML.
It turns the format-pretty-print feature on by default which produces
the result you see (BTW, I believe it adds extra new lines only for
element that are directly under the root so real documents with several
nesting levels actually look better with these extra newlines than
without them).
You only other option is to turn pretty-printing off altogether in which
case you will get unformatted XML. To do this you will need to set the
DOM-to-XML serialization stage yourself and switch the
format-pretty-print feature off on the DOMWriter object. For more
information see Q3.2 in the C++/Tree Mapping FAQ:
http://wiki.codesynthesis.com/Tree/FAQ
As well as the Xerces-C++ DOMWriter documentation:
http://xerces.apache.org/xerces-c/program-dom.html#DOMWriterFeatures
Boris
More information about the xsd-users
mailing list