Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:14:55+00:00 2026-06-14T05:14:55+00:00

I am using org.simpleframework.xml (http://simple.sourceforge.net/) to serialize Java Objects to XML. What I would

  • 0

I am using org.simpleframework.xml (http://simple.sourceforge.net/) to serialize Java Objects to XML.

What I would like to add is to add a comments area in the resulting XML, based on Annotations in the Java object.

So for example I would like to write some Java Object like:

@Root(name = "myclass")
public class MyClass {
  @Element(required=true)
  @Version(revision=1.1)
  @Comment(text=This Element is new since, version 1.1, it is a MD5 encrypted value)
  private String activateHash;
}

And the resulting xml would look like:

<myclass version="1.1">
  <!-- This Element is new since, version 1.1, it is a MD5 encrypted value -->
  <activateHash>129831923131s3jjs3s3jjk93jk1</activateHash>
</myclass>

There is an example in their docs on howto write a Visitor that will write a comments in the xml:
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#intercept

However: How can I attach a Visitor to a Strategy at all?

And further the Visitor concept of simpleframework does not allow access to the raw parsing class.
In the Visitor there is only a method to overwrite:

public void write(Type type, NodeMap<OutputNode> node) { ... }

=> OutputNode does not give me a chance to read the Annotation of the Element that I am parsing. So how should one access the Annotations of the attribute.

Thanks!

Sebastian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:14:57+00:00Added an answer on June 14, 2026 at 5:14 am

    Update as of 2012-11-05:

    Answer by the author of org.simpleframework.xml:
    This works

    https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/strategy/CommentTest.java

    package org.simpleframework.xml.strategy;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    import org.simpleframework.xml.Default;
    import org.simpleframework.xml.Root;
    import org.simpleframework.xml.ValidationTestCase;
    import org.simpleframework.xml.core.Persister;
    import org.simpleframework.xml.stream.InputNode;
    import org.simpleframework.xml.stream.NodeMap;
    import org.simpleframework.xml.stream.OutputNode;
    
    public class CommentTest extends ValidationTestCase {
    
       @Retention(RetentionPolicy.RUNTIME)
       private static @interface Comment {
          public String value();
       }
    
       @Root
       @Default
       private static class CommentExample {
          @Comment("This represents the name value")
          private String name;
          @Comment("This is a value to be used")
          private String value;
          @Comment("Yet another comment")
          private Double price;
       }
    
       private static class CommentVisitor implements Visitor {
          public void read(Type type, NodeMap<InputNode> node) throws Exception {}
          public void write(Type type, NodeMap<OutputNode> node) throws Exception {
             if(!node.getNode().isRoot()) {
                Comment comment = type.getAnnotation(Comment.class);
                if(comment != null) {
                   node.getNode().setComment(comment.value());
                }
             }
          }
       }
    
       public void testComment() throws Exception {
          Visitor visitor = new CommentVisitor();
          Strategy strategy = new VisitorStrategy(visitor);
          Persister persister = new Persister(strategy);
          CommentExample example = new CommentExample();
    
          example.name = "Some Name";
          example.value = "A value to use";
          example.price = 9.99;
    
          persister.write(example, System.out);
       }
    
    }
    

    Update as of 2012-11-01 20:16

    this is the workaround that seems to get the desired effect – the necessary FieldHelper is described in (Get the value of a field, given the hierarchical path)

        /**
         * write according to this visitor
         */
        public void write(Type type, NodeMap<OutputNode> node) {
            OutputNode element = node.getNode();
            Class ctype = type.getType();
    
            String comment = ctype.getName();
            if (!element.isRoot()) {
                FieldHelper fh = new FieldHelper();
                element.setComment(comment);
                try {
                    if (type.getClass().getSimpleName().startsWith("Override")) {
                        type = (Type) fh.getFieldValue(type, "type");
                    }
                    if (type.getClass().getSimpleName().startsWith("Field")) {
                        Field field = (Field) fh.getFieldValue(type, "field");
                        System.out.println(field.getName());
                        Comment commentAnnotation = field.getAnnotation(Comment.class);
                        if (commentAnnotation != null) {
                            element.setComment(commentAnnotation.value());
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    

    Here is how far I got with this. Unfortunately it does not work as expected. I have written an E-Mail to the author of the Simpleframwork for XML.

      /**
      * write according to this visitor
      */
        public void write(Type type, NodeMap<OutputNode> node) {
            OutputNode element = node.getNode();
            Class ctype = type.getType();
    
            String comment = ctype.getName();
            if (!element.isRoot()) {
                Comment commentAnnotation = type.getAnnotation(Comment.class);
                if (commentAnnotation!=null)
                    element.setComment(commentAnnotation.value());
                else
                    element.setComment(comment);
            }
        }
    
        @Override
        public void read(Type type, NodeMap<InputNode> nodeMap) throws Exception {
    
        }
    
    }
    

    I declared the Comment annotation like this:

    package com.bitplan.storage.simplexml;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.annotation.ElementType;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Comment {
    String value();
    }
    

    which is then usable like this:

    @Comment("this is the unique identifier")
    private long id;
    

    adding the Visitor was possible like this:

    /**
     * get Serializer
     * 
     * @return
     */
    public Serializer getSerializer() {
        Serializer serializer = null;
        Strategy strategy=null;
        VisitorStrategy vstrategy=null;
        if ((idname != null) && (refname != null)) {
            strategy = new CycleStrategy(idname, refname);
        }
        CommentVisitor cv=new CommentVisitor();
        if (strategy==null) {
            vstrategy=new VisitorStrategy(cv);
        } else {
            vstrategy=new VisitorStrategy(cv,strategy);
        }       
        serializer = new Persister(vstrategy);
        return serializer;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using com.sun.org.apache.xml.internal.serialize.XMLSerializer and com.sun.org.apache.xml.internal.serialize.OutputFormat causes some errors when compiling using java 1.6. The solution
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I am using org.apache.http.impl.client.DefaultHttpClient to retrieve xml from a webservice and am trying to
How can I serialize java.util.concurrent.TimeUnit with the Simple XML lib (version 2.6.5 / 2.6.6)?
I'm using org mode to make a list that I would like exported to
i am using org.w3c.dom in java to read an xml with the following function
I'm stuck with parsing JSON in Java using org.json. My JSON looks like this
How to remove XML nodes in Java SE? I am using org.apache.xerces . Thanks.
I am using org.apache.commons.validator.routines.DateValidator to validate a date with a simple date pattern dd/mm/yyyy
I am using org.tuckey.web.filters.urlrewrite.UrlRewriteFilter on my java project.It works fine .My issue is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.