[jdom-interest] XMLOutputter.output() and xml:lang attribute results in lang output

Butt, Vaughn A. vaughn.butt at nz.unisys.com
Mon Dec 9 13:34:58 PST 2002


>I want to the "xml:" included in my output.
I've solved this by using the SAXBuilder instead of the DOMBuilder

Thank you Alex.



Alex Rosen wrote
> Maybe DOMBuilder is not doing the right thing. If you parse the
> JDOM document directly from a file, does it work? If you use
> DOMBuilder, and you loop over the element's attributes, do you
> see the xml:lang attribute?

I think you are right about DOMBuilder not doing the right thing.
Parsing the document from file using DOMbuilder.build(File) (the
deprecated method) give the same erroneous(?) results.  Using
SAXBuilder.build(File) and SAXBuilder.build(InputStream) results
in the out put I want.

As for looping over the attributes I see xml:lang when I use
SAXBuilder and just lang when I use DOMBuilder.  I wonder which
is the correct behaviour?

This element:

<grammar xml:lang="en-US" mode="voice" root="id" version="1.0">
<rule id="id" scope="public">             <item>(wibble)</item>
</rule>     </grammar>


and this code

            System.out.println("element:'" + element + "'");
            List list = element.getAttributes();
            for (Iterator iterator = list.iterator() ; iterator.hasNext() ;)
{
                Attribute attribute = (Attribute)iterator.next();
                System.out.println("attribute:'" + attribute + "'");
                System.out.println("attribute.getName():'" +
attribute.getName() + "'");
                System.out.println("attribute.getNamespace():'" +
attribute.getNamespace() + "'");
                System.out.println("attribute.getNamespacePrefix():'" +
attribute.getNamespacePrefix() + "'");
                System.out.println("attribute.getNamespaceURI():'" +
attribute.getNamespaceURI() + "'");
            }

generates this output when I use DOMBuilder

element:'[Element: <grammar/>]'
attribute:'[Attribute: mode="voice"]'
attribute.getName():'mode'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''
attribute:'[Attribute: root="id"]'
attribute.getName():'root'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''
attribute:'[Attribute: version="1.0"]'
attribute.getName():'version'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''
attribute:'[Attribute: lang="en-US"]'
attribute.getName():'lang'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''

and this output when I use SAXBuilder

element:'[Element: <grammar/>]'
attribute:'[Attribute: mode="voice"]'
attribute.getName():'mode'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''
attribute:'[Attribute: root="id"]'
attribute.getName():'root'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''
attribute:'[Attribute: version="1.0"]'
attribute.getName():'version'
attribute.getNamespace():'[Namespace: prefix "" is mapped to URI ""]'
attribute.getNamespacePrefix():''
attribute.getNamespaceURI():''
attribute:'[Attribute: xml:lang="en-US"]'
attribute.getName():'lang'
attribute.getNamespace():'[Namespace: prefix "xml" is mapped to URI
"http://www.w3.org/XML/1998/namespace"]'
attribute.getNamespacePrefix():'xml'
attribute.getNamespaceURI():'http://www.w3.org/XML/1998/namespace'

-----Original Message-----
From: Alex Rosen [mailto:arosen at novell.com]
Sent: Tuesday, 10 December 2002 9:07 a.m.
To: jdom-interest at jdom.org; vaughn.butt at nz.unisys.com
Subject: Re: [jdom-interest] XMLOutputter.output() and xml:lang
attribute results in lang output


I would think that your code should work. A couple of suggestions:

- Maybe DOMBuilder is not doing the right thing. If you parse the JDOM
document directly from a file, does it work? If you use DOMBuilder, and you
loop over the element's attributes, do you see the xml:lang attribute?

- Any reason you're outputting the root element, instead of the document? I
guess it's possible that that's causing the problem. Also, if your document
has any comments or PIs before or after the root element, they'll get lost
with your code.

Alex

>>> "Butt, Vaughn A." <vaughn.butt at nz.unisys.com> 12/08/02 11:45PM >>>
I am trying to get an org.jdom.Element as a string by using
org.jdom.XMLoutput(Element element,Writer writer) where the writer is a
StringWriter.

My problem is that the source elements with an attribute xml:lang="en-US" in
it are being streamed to my StringWriter as an element with plain
lang="en-US".

I checked the FAQ (http://jdom.org/docs/faq.html) and found that "The
xml:lang and xml:space attributes are special cases that are allowed..." but
it does not say how JDOM deals with them.

I want to the "xml:" included in my output.

Can I do this (eg by set some attribute on XMLOutputter)?

If I can how is it done?

If I can't then please let me know.

TIA (hopefully)
Vaughn Butt
Software Developer


PS. Here is the (rather ugly and slightly incomplete) code that I want to be
able to get this output from.

public class Migration {
    public static void main(String[] args) {
        org.w3c.dom.Document domDocument = methodThatReturnsADOMDocument();

        org.jdom.Document doc = new DOMBuilder().build(domDocument);

        System.out.println(element2String(doc.getRootElement());
    }

    private static String element2String(Element element) {
        String retVal = null;
        XMLOutputter outputter = new XMLOutputter("    ",true);
        try {
            StringWriter sw = new StringWriter();
            outputter.output(element,sw);
            retVal = sw.toString();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return retVal;
    }
}

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com



More information about the jdom-interest mailing list