[jdom-interest] XMLOutputter wihout all attributes

Per Norrman pernorrman at telia.com
Sat Apr 24 15:07:15 PDT 2004


Hi,

Not as such. You'll have to subclass XMLOutputter and provide your own
implementation of the protected printAttributes method.

This question was up a couple of months ago, and I suggested extracting 
the main part of the loop inside printAtrributes to a printAttribute 
method. This would facilitate the following:

public class DiscrimintaingOutputter extends XMLOutputter {
    protected void printAttribute(
       Writer out, NamespaceStack namespaces, Attribute attribute)
       throws IOException
    {
       if (!attribute.getName().startsWith("a")) {
          super.printAttribute(out, namespaces, attribute);
       }
    }

    static String xml =
       "<stuff a='1' b='2'><other aa='xyzzy' bb='stump' /></stuff>";

    public static void main(String[] args) throws Exception {
       Document doc = new SAXBuilder().build(new StringReader(xml));
       new XMLOutputter().output(doc, System.out);
       // Skip all attributes that begings with the character 'a'
       new DiscrimintaingOutputter().output(doc, System.out);
    }
}

A patch is attached.

/pmn



Martin Pöpping wrote:

> Hello,
> 
> I want to output my XML-Document as String,
> but I do not want that some attributes of my elements are printed out in 
> that String.
> 
> Is there a way set up the XMLOutputter to ignore some attributes?
> 
> 
> Thanks a lot for help,
> 
> Martin
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com 
> 
> 
-------------- next part --------------
Index: XMLOutputter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/output/XMLOutputter.java,v
retrieving revision 1.110
diff -r1.110 XMLOutputter.java
1114,1126c1114,1115
<             Namespace ns = attribute.getNamespace();
<             if ((ns != Namespace.NO_NAMESPACE) &&
<                 (ns != Namespace.XML_NAMESPACE)) {
<                     printNamespace(out, ns, namespaces);
<             }
< 
<             out.write(" ");
<             printQualifiedName(out, attribute);
<             out.write("=");
< 
<             out.write("\"");
<             out.write(escapeAttributeEntities(attribute.getValue()));
<             out.write("\"");
---
> 			Namespace ns;
> 			printAttribute(out, namespaces, attribute);
1128a1118,1146
> 
>     /**
>      * Print one <code>{@link Attribute}</code> on the designated
>      * Writer.
>      * @param out
>      * @param namespaces
>      * @param attribute
>      * @throws IOException
>      */
> 	protected void printAttribute(
> 		Writer out,
> 		NamespaceStack namespaces,
> 		Attribute attribute)
> 		throws IOException {
>             
> 		Namespace ns = attribute.getNamespace();
> 		if ((ns != Namespace.NO_NAMESPACE) &&
> 		    (ns != Namespace.XML_NAMESPACE)) {
> 		        printNamespace(out, ns, namespaces);
> 		}
> 		
> 		out.write(" ");
> 		printQualifiedName(out, attribute);
> 		out.write("=");
> 		
> 		out.write("\"");
> 		out.write(escapeAttributeEntities(attribute.getValue()));
> 		out.write("\"");
> 	}


More information about the jdom-interest mailing list