[xsde-users] Extract remaining subtree
Boris Kolpackov
boris at codesynthesis.com
Mon Aug 3 08:50:45 EDT 2009
Hi Markus,
Markus Kohler <MarkusK at microsol.ie> writes:
> given a XML file and using the embedded parser xsde can I retrieve a
> certain part of the XML tree as string. Example:
>
> <somexml>
> <somelement>
> <subtree>
> <I>
> <want/>
> <this/>
> <subtree-as-string/>
> </I>
> </subtree>
> </someelement>
> </somexml>
The easiest way would be to override the low-level XML parsing
callbacks and re-serialize the XML fragment to string, something
along these lines:
class someelement_pimpl: public someelement_pskel
{
public:
virtual void
pre ()
{
str_.clear ();
}
virtual void
_start_element (const xml_schema::ro_string& ns,
const xml_schema::ro_string& name)
{
str_ += "<";
str_ += name;
open_ = true;
}
virtual void
_end_element (const xml_schema::ro_string& ns,
const xml_schema::ro_string& name)
{
if (open_)
{
str_ += "/>";
open_ = false;
}
else
{
str_ += "</";
str_ += name;
str_ += ">";
}
}
virtual void
_attribute (const xml_schema::ro_string& ns,
const xml_schema::ro_string& name,
const xml_schema::ro_string& value)
{
str_ += " ";
str_ += name;
str_ += " = \"";
str_ += value;
str_ += "\"";
}
virtual void
_characters (const xml_schema::ro_string& s)
{
if (open_)
{
str_ += ">";
open_ = false;
}
str_ += s;
}
virtual void
post_someelement ()
{
// fragment is in str_
}
private:
bool open_;
string str_;
};
Note that this will only work for simple XML which doesn't contain
special characters that need escaping. If you need a more robust
solution then you can use the underlying XML serializer that is
used in the C++/Serializer mapping. See libxsde/xsde/c/genx/ for
more information.
Boris
More information about the xsde-users
mailing list