commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject Re: How to parse an attribute whoes value is a QName
Date Fri, 03 Mar 2006 02:59:53 GMT
On Thu, 2006-03-02 at 18:16 -0800, Michelle Lin wrote:
> Digester Experts:
> 
> I have the following simple XML doc:
> 
> <?xml version="1.0" encoding="ASCII"?>
> <xyz xmlns="http://mynamespace"               
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>     <property name="a" type="xsd:string"/>
> </xyz>
> 
> How can I use the digester to parse this XML doc and
> get the value of "type" attribute as QName of:
> namespace: "http://www.w3.org/2001/XMLSchema"
> prefix: "xsd"
> localPart: "string"
> 
> Any suggestions/pointers are very much appreciated.

Method Digester.findNamespaceURI can map from the string "xsd" to the
associated URI.

In order to call it at the correct time I would recommend creating a
custom Rule. eg:

// when this rule fires, if the current element has an attribute
// of the specified name, then it is assumed to be a prefix:ename
// pair. It is converted into a QName object and passed to a bean
// property (specified by propName) on the top object on the
// digester stack

public class SetQNameRule extends Rule {
  private String attrName;
  private String propName;

  public SetQNameRule(String attrName, String propName) {
    this.attrName = attrName;
    this.propName = propName;
  }

  public void begin(namespace, name, Attributes attrs) {
    // get attribute with name this.attrName
    // split it on ':' char
    // call digester.findNamespaceURI to convert prefix to URI
    // create QName object
    //
    // get top object on digester stack
    // Use BeanUtils to assign the qname to its propName property
  }
}

The above is basically a simplified version of CallMethodRule.

As an alternative, create a Rule based on CallParamRule. Of course
you'll then need to use your rule in combination with a CallMethodRule.

Another alternative would be to register a custom (String->QName)
converter with the BeanUtils framework. That's a bit scary though as the
converter needs a reference back to the Digester instance in order to
map prefix->URI but BeanUtils converters are effectively "global". If
you've got a multithreaded app things could go badly wrong.

Hope this helps.

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message