There are a couple of ways to do that, depending on your data.  I'll
give an outline of three ways; if none of them fit, or you need more
information, just ask.  See also
http://pyxb.sourceforge.net/arch_content.html#associating-xml-and-python-objects

(1) Tags and attributes are declared in the schema:

Every Python object that represents an element is associated with a
complex type which is represented by a class.  These classes have two
internal class variables:

* _AttributeMap is a dictionary where keys are instances of ExpandedName
and values are instances of AttributeUse.

* _ElementMap is a dictionary where keys are instances of ExpandedName
and values are instances of ElementUse.

These can be used to locate values by name.  Be aware that because they
use ExpandedName
(http://pyxb.sourceforge.net/arch_namespaces.html#expanded-names), you
will not be able to lookup tags by just the local name if the element is
qualified within its containing component.

(NB: I should provide a public interface to this functionality, but
haven't yet.)

The use instances encode information about how to retrieve the value of
the corresponding attribute or element from a binding instance.  So, if
you want to iterate through all the elements (attributes) in an
instance, you can do:

for (k, eu) in instance.typeDefinition()._ElementMap.items():
  value = eu.value(instance)

Note that the returned value will be a list if the content model allows
multiple instance of the element, or if multiple elements have the same
local name.  Invoking eu.value(instance) is exactly what happens when
you use the property interface to dereference instance.tag.

Your example implies that the elements have either mixed content, or
simple content.  In the latter case, you may need to use the value()
method on the instance.  See examples/content/showcontent.py in the
distribution.

(2) Tags and attributes are wildcards from the schema:

The above assumes that your elements are declared in the schema.  If
instead they are recognized as wildcards, you use the wildcardElements()
method to get a list of those in the order they were encountered in the
document, and wildcardAttributeMap() to get a map from ExpandedName to
the value of the unrecognized attribute.  See
tests/drivers/test-wildcard.py:TestWildcard for examples.

The values in wildcardElements() are either Python instance documents
(if the element could be recognized and translated into a binding
instance), or instances of a restricted xml.dom.Node class.

(3) Extracting from mixed content:

If the body of an element has mixed content, use the content() method on
the instance to get a list of alternating text and element values, in
document order.  Something like:

for c in instance.content():
  if isinstance(c, pyxb.binding.basis._TypeDefinition_mixin):
    # Element content
    pass
  else:
    # Text content
    pass

./bugs/test-200907231924.py has an example.

The content() method should always contain, in order, the elements and
text of the element's content, as long as the instance was created by
PyXB from a document.  When instances are created or mutated manually,
the content() sequence may not be accurate.

Peter

Salvatore Leone wrote:
> Hi,
>
> I'm using pyxb and seems great, but I have a question.
>
> I need to get some arbitrary attribute from my xml document. so, if I 
> have this xml line:
>
> ...
> <tag1 attribute="attribute_value" ...>tag_value</tag1>
> <tag2 attribute="attribute_value" ...>tag_value</tag2>
> ...
>
> I need a method for checking the tag_values against another value. Like 
> this:
>
> for tag_name in tags:
>     tag_value = xml.get(tag_name)
>
>
> So, as you see I can't do something like xml.tag1, becouse I don't know 
> which tag do I have to check...
> and will be very helpful something similar for attributes  
> (tag1.get(attribute_name))
>
> Is this possible with pyxb? Am I missing something?
>
>
> Regards,
> Salvatore
>
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay 
> ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
> http://p.sf.net/sfu/devconf
> _______________________________________________
> pyxb-users mailing list
> pyxb-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pyxb-users
>   




------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
pyxb-users mailing list
pyxb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyxb-users

Reply via email to