[xsde-users] Get the currently parsed element's name

Ninh Tran Dang tdninh at tma.com.vn
Fri Oct 31 01:01:55 EDT 2008


Hi Boris,

We tried with your new suggestion, it's ok when retrieving the element's
name from the pre() with the "const char* name = _context ().element_name_"
statement. But it got the segmentation fault when getting this from the
post() method. Even we tried in the post() of element that doesn't contain
the nested elements.

Do you know the reason why we got this error?

Thanks,
Ninh

-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: Wednesday, October 29, 2008 3:37 PM
To: Khuong Nguyen Thi Lien
Cc: xsde-users at codesynthesis.com; 'Ninh Tran Dang'; 'Duy Chau Hai'; 'Thien
Le Minh'; gqminh at tma.com.vn
Subject: Re: [xsde-users] Get the currently parsed element's name

Hi Khuong,

Khuong Nguyen Thi Lien <ntlkhuong at tma.com.vn> writes:

> The second approach is what we current apply, but we are optimizing the
code
> size, so we try to minimize the consumed number of methods to create the
> object right at the time it is being parsed and the second approach takes
us
> some steps to create the object including init and set value actions. The
> first approach could help, but it needs us a lot of effort on creating the
> connections between parser implementation classes which could reach to
> thousands of classes in quantity. We are trying to figure out another
> approach based on your explanation, otherwise we will apply the second
> approach.

My understanding was that you have just a few places where you need
element names. If you need it in every parser and the number of
parsers is large then the suggested two approaches can require
quite a bit of work. In this case another approach can be a better
fit. Its main disadvantage is that it requires a few modifications
to the XSD/e runtime.

There is a notion of parsing context that is available to all parsers
via the _context() function. The context class is defined in the 
libxsde/xsde/cxx/parser/context.* files. We can modify this context
class to also include the current element name. For example we can
add it after the xml_parser_ variable at line 131 (using the XSD/e 
2.1.0 source code):

protected:
  XML_Parser xml_parser_;

public:
  const char* element_name_;
};

The other change that we need to make is to set this name. For that
we need to add the following line in 
libxsde/xsde/cxx/parser/expat/document.cxx, after line 593:

context_.element_name_ = name_p;

Now we can implement inner_pimpl like so:

struct inner_pimpl: inner_pskel
{
  virtual void
  pre ()
  {
    const char* name = _context ().element_name_;
  }
};

Note also that with this approach _context ().element_name_ always
contains the latest element seen. So for example if inner contained
a nested element then in inner_post() _context ().element_name_ would
contain this nested element's name, not the one that we need. In other
words, you need to save the element name in pre() unless the type has
no nested elements.

Let me know if you run into any problems with this.

Boris



More information about the xsde-users mailing list