Re: Minor enhancements to XmlPullWrapper and XmlSerializerWrapper
Aleksander Slominski <[email protected]> Thu, 06 Mar 2003 11:45:22 -0500
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
Naresh Bhatia wrote:
> Hi Alek,
>
hi!
> I have made a minor enhancement to these wrappers to recognize the
xsi:nil attribute. Do you think this should go in to the utility
wrappers you are writing?
this looks like good additions and i will make sure they are in wrapper
interfaces.
i want to make as easy as possible to add new extensions (such as yours)
and seamlessly integrate them into XmlPullParser/Serializer APIs and
that requires pull/serializer wrapper mechanism to more more powerful.
currently i am factoring common functionality required in dynamic
proxies for parser and serializer and adding automatic conversion from
static methods to instance methods so Util code with static methods
(preferred in J2ME) can be reused with dynamic proxies (when JDK 1.3 or
better is available).
should have soon first version working that i will check in (will need
more work on performance tuning though ...).
thanks,
alek
> Thanks.
> Naresh
>
> public class XmlPullWrapper {
>
> static final String XSI_NS="http://www.w3.org/2001/XMLSchema-instance";
> …
>
> /**
> * Read the text of a required element and return it or throw exception if
> * required element is not found. Useful for getting the text of simple
> * elements such as <username>johndoe</username>. Assumes that parser is
> * just before the start tag and leaves the parser at the end tag. If the
> * text is nil (e.g. <username xsi:nil="true"/>), then a null will be
> returned.
> */
>
> public String getRequiredElementText(String namespace, String name)
> throws IOException, XmlPullParserException {
> if (name == null) {
> throw new XmlPullParserException("name for element can not be null");
> }
>
> String text = null;
> nextStartTag(namespace, name);
> if (isNil()) {
> nextEndTag(namespace, name);
> }
> else {
> text = parser.nextText();
> }
> parser.require(XmlPullParser.END_TAG, namespace, name);
> return text;
> }
>
> /**
> * Is the current tag nil? Checks for xsi:nil="true".
> */
> public boolean isNil()
> throws IOException, XmlPullParserException {
>
> boolean result = false;
> String value = parser.getAttributeValue(XSI_NS, "nil");
> if ("true".equals(value)) {
> result = true;
> }
>
> return result;
> }
> }
>
> public class XmlSerializerWrapper {
>
> static final String XSI_NS="http://www.w3.org/2001/XMLSchema-instance";
>
> /**
> * The serializer used to write XML output. It is the responsibility of
> the subclass
> * to initialize this member.
> */
> protected XmlSerializer serializer;
>
> /**
> * Writes a simple element such as <username>johndoe</username>. The
> namespace
> * and elementText are allowed to be null. If elementText is null, an
> xsi:nil="true"
> * will be added as an attribute.
> */
> public void writeSimpleElement(String namespace, String elementName,
> String elementText)
> throws IOException, XmlPullParserException {
>
> if (elementName == null) {
> throw new XmlPullParserException("name for element can not be null");
> }
>
> serializer.startTag(namespace, elementName);
> if (elementText == null) {
> serializer.attribute(XSI_NS, "nil", "true");
> }
> else {
> serializer.text(elementText);
> }
> serializer.endTag(namespace, elementName);
> }
> }
>
>
> *Yahoo! Groups Sponsor*
> ADVERTISEMENT
> <http://rd.yahoo.com/M=248676.3045811.4372626.2848452/D=egroupweb/S=1706030390:HM/A=1481290/R=0/*http://webhosting.yahoo.com/ps/wh3/prod/>
>
>
>
> To unsubscribe from this group, send an email to:
> [email protected]
>
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
> <http://docs.yahoo.com/info/terms/>.
--
"Mr. Pauli, we in the audience are all agreed that your theory is crazy.
What divides us is whether it is crazy enough to be true." Niels H. D. Bohr
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/LIgTpC/vN2EAA/xGHJAA/2U_rlB/TM
---------------------------------------------------------------------~->
To unsubscribe from this group, send an email to:
[email protected]
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/