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

  • Home
  • SEARCH
  • 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 8787003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:46:34+00:00 2026-06-13T21:46:34+00:00

Not new to Java; but relatively new to XML-parsing. I know a tiny bit

  • 0

Not new to Java; but relatively new to XML-parsing. I know a tiny bit about a lot of the XML tools out there, but not much about any of them. I am also not an XML-pro.

My particular problem is this… I have been given an XML-document which I cannot modify and from which I need only to parse random bits of it into Java objects. Sheer speed is not much of a factor so long as it’s reasonable. Likewise, memory-footprint need not be absolutely optimal either, just not insane. I only need to read through the document one time to parse it, after that I’ll be throwing it in the bitbucket and just using my POJO.

So, I’m open to suggestion… which tool would you use?
And, would you kindly suggest a bit of starter-code to address my particular need?

Here’s a snippet of sample XML and the associated POJO I’m trying to craft:

<xml>
  <item id="...">
    ...
  </item>
  <metadata>
    <resources>

      <resource>
        <ittype>Service_Links</ittype>
        <links>
          <link>
            <path>http://www.stackoverflow.com</path>
            <description>Stack Overflow</description>
          </link>
          <link>
            <path>http://www.google.com</path>
            <description>Google</description>
          </link>
        </links>
      </resource>

      <resource>
        <ittype>Article_Links</ittype>
        <links>
          ...
        </links>
      </resource>

      ...

    </resources>
  </metadata>
</xml>


public class MyPojo {

    @Attribute(name="id")
    @Path("item")
    public String id;

    @ElementList(entry="link")
    @Path("metadata/resources/resource/links")
    public List<Link> links;
}

NOTE: this question was originally spawned by this question with me trying to solve it using SimpleXml; I’m to the point where I thought maybe someone could suggest a different route to solving the same problem.

Also Note: I’m really hoping for a CLEAN solution… by which I mean, using annotations and/or xpath with the least amount of code… the last thing I want is huge class file with huge unwieldy methods… THAT, I already have… I’m trying to find a better way.

😀

  • 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-13T21:46:35+00:00Added an answer on June 13, 2026 at 9:46 pm

    OK, so I settled on a solution that (to me) seemed to address my needs in the most reasonable way. My apologies to the other suggestions, but I just liked this route better because it kept most of the parsing-rules as annotations and what little procedural-code I had to write was very minimal.

    I ended up going with JAXB; initially I thought JAXB would either create XML from a Java-class or parse XML into a Java-class but only with an XSD. Then I discovered that JAXB has annotations that can parse XML into a Java-class without an XSD.

    The XML-file I’m working with is huge and very deep, but I only need bits and bites of it here and there; I was worried that navigating what maps to where in the future would be very difficult. So I chose to structure a tree of folders modeled after the XML… each folder maps to an element and in each folder is a POJO representing that actual element.

    Problem is, sometimes there is an element who has a child-element several levels down which has a single property I care about. It would be a pain to create 4 nested-folders and a POJO for each just to get access to a single property. But that’s how you do it with JAXB (at least, from what I can tell); once again I was in a corner.

    Then I stumbled on EclipseLink’s JAXB-implementation: Moxy.
    Moxy has an @XPath annotation that I could place in that parent POJO and use to navigate several levels down to get access to a single property without creating all those folders and element-POJOs. Nice.

    So I created something like this:
    (note: I chose to use getters for cases where I need to massage the value)

    // maps to the root-"xml" element in the file
    @XmlRootElement( name="xml" )
    @XmlAccessorType( XmlAccessType.FIELD )
    public class Xml {
    
        // this is standard JAXB
        @XmlElement;               
        private Item item;
        public Item getItem() {    
            return this.item;
        }
    
        ...
    }
    
    // maps to the "<xml><item>"-element in the file
    public class Item {
    
        // standard JAXB; maps to "<xml><item id="...">"
        @XmlAttribute              
        private String id;
        public String getId() {
            return this.id;
        }
    
        // getting an attribute buried deep down
        // MOXY; maps to "<xml><item><rating average="...">"
        @XmlPath( "rating/@average" )    
        private Double averageRating;
        public Double getAverageRating() {
            return this.average;
        }
    
        // getting a list buried deep down
        // MOXY; maps to "<xml><item><service><identification><aliases><alias.../><alias.../>"
        @XmlPath( "service/identification/aliases/alias/text()" )
        private List<String> aliases;
        public List<String> getAliases() {
            return this.aliases;
        }
    
        // using a getter to massage the value
        @XmlElement(name="dateforindex")
        private String dateForIndex;
        public Date getDateForIndex() {
            // logic to parse the string-value into a Date
        }
    
    }
    

    Also note that I took the route of separating the XML-object from the model-object I actually use in the app. Thus, I have a factory that transforms these crude objects into much more robust objects which I actually use in my app.

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

Sidebar

Related Questions

I'm new to Java but not to programming. I'm reading the book Beginning Android
First, i'm new at Java-programming and my native lang is not english, but still
I'm relatively new to Java EE and have already began to hear about the
i am relatively experienced in Java coding but am new to C++. I have
I'm still relatively new to Java. I've tried searching for a solution, but I
I am relatively new to Java and while trying out some code came across
I'm relatively new to concurrency in Java (still have yet to read JCIP, but
I'am relatively new to java and want to try out something new. At the
Ok I am relatively new to Java Programming, but have previous experience in C++.
I'm relatively new to Java and decided to mess around a bit with Swing.

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.