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 634925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:20:56+00:00 2026-05-13T20:20:56+00:00

I have an XML document (generated by Adobe XFA forms), that contains data like

  • 0

I have an XML document (generated by Adobe XFA forms), that contains data like the following:

<Position>
   <PositionBorder>
       <Title/>
       <StartDate/>
       <EndDate/>
   </PositionBorder>
</Position>

Since this file is defined elsewhere, I am not at liberty to change the format of the XML that I get.

In my Java code, I create a Position class that contains the Title, Start and End Dates.

My problem is, when I use XStream to parse the file, it wants a PositionBorder class to hold the title and dates. I want to basically ignore the border and place all of the fields into the Position class.

What I’d really like to do is use something like the convertAnother method to convert the child of the position element. I tried to do just that and it fails, because my PositionConverter gets called for the PositionBorder (when I call convertAnother).

Anyone have any clues how to deal with collapsing the structure of an XML when parsing?

  • 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-05-13T20:20:56+00:00Added an answer on May 13, 2026 at 8:20 pm

    It’s not terribly difficult to do with a custom converter. This is a little bit of a long example, but I hope it’s simple enough to get the gist of what you need to do:

    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    import com.thoughtworks.xstream.converters.Converter;
    import com.thoughtworks.xstream.converters.MarshallingContext;
    import com.thoughtworks.xstream.converters.UnmarshallingContext;
    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
    
    public final class ConverterTest {
        public static void main(String[] args) {
            XStream xstream = new XStream();
            xstream.autodetectAnnotations(true);
            xstream.registerConverter(new PositionConverter());
    
            final Position position = new Position();
            position.setTitle("The Title");
            position.setStartDate("The Start Date");
            position.setEndDate("The End Date");
    
            final String xml = xstream.toXML(position);
            System.out.println("Generated XML:");
            System.out.println(xml);
    
            final Position genPosition = (Position) xstream.fromXML(xml);
            System.out.println("Generated Position:");
            System.out.println("\tTitle: " + genPosition.getTitle());
            System.out.println("\tStart Date: " + genPosition.getStartDate());
            System.out.println("\tEnd Date: " + genPosition.getEndDate());
        }
    
        @XStreamAlias("Position")
        private static class Position {
            public String getEndDate() {
                return endDate;
            }
    
            public void setEndDate(String endDate) {
                this.endDate = endDate;
            }
    
            public String getStartDate() {
                return startDate;
            }
    
            public void setStartDate(String startDate) {
                this.startDate = startDate;
            }
    
            public String getTitle() {
                return title;
            }
    
            public void setTitle(String title) {
                this.title = title;
            }
    
            private String title;
            private String startDate;
            private String endDate;
        }
    
        private static class PositionConverter implements Converter {
            public boolean canConvert(Class clazz) {
                return Position.class == clazz;
            }
    
            public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
                Position position = (Position)value;
                writer.startNode("PositionBorder");
    
                writer.startNode("Title");
                writer.setValue(position.getTitle());
                writer.endNode();
    
                writer.startNode("StartDate");
                writer.setValue(position.getStartDate());
                writer.endNode();
    
                writer.startNode("EndDate");
                writer.setValue(position.getEndDate());
                writer.endNode();
    
                writer.endNode();
            }
    
            public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
                Position position = new Position();
                // move it to <PositionBorder> tag.
                reader.moveDown();
                // now move it to <Title> tag.
                reader.moveDown();
                String title = reader.getValue();
                position.setTitle(title);
                reader.moveUp(); // moves back to <PositionBorder>
    
                reader.moveDown(); // should move down to <StartDate> tag
                String startDate = reader.getValue();
                position.setStartDate(startDate);
                reader.moveUp(); // move back to <PositionBorder>
    
                reader.moveDown(); // should move down to <EndDate> tag
                String endDate = reader.getValue();
                position.setEndDate(endDate);
                reader.moveUp(); // move back to <PositionBorder>
    
    
                return position;
            }
        }
    }
    

    Try running that and see what happens. You’ll need to modify it to suit your own types, of course — I just used strings for all of Position’s fields (and I’m sure you’re Position class isn’t nested, either), but converting from a String to a Date (or whatever) should be rather trivial.

    One thing you’ll want to keep an eye on (and I might not have gotten it completely right in my example) is matching your reader.moveDown() and reader.moveUp() calls. (And, if you’re going to do any marshalling instead of just unmarshalling — which I don’t expect from your question — you’ll want to match your writer.startNode() and writer.endNode() calls as well.) It probably won’t cause any problems with this example, but I’m sure it’ll raise issues if you’re doing anything larger or processing multiple files with the same XStream or Converter instance. Also, if you try reader.moveDown() from an invalid location, you’ll get a really ugly exception — it should be pretty obvious.

    I had to play around with the moveUp/moveDown methods a bit to get them in the right places, so I’m sure you’ll need to test it and tweak it until you get what you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a few server-side options, there's a project on… May 14, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer Most likely there are a lot of libraries that parse… May 14, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer Try this: RewriteEngine on RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ RewriteRule ^$ http://www.example.com/index.php… May 14, 2026 at 9:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.