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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:14:29+00:00 2026-06-03T22:14:29+00:00

I am wondering what’s the best practice to parse XML like this: <root> <MailNotification

  • 0

I am wondering what’s the best practice to parse XML like this:

<root>
    <MailNotification enable="true">
        <To>foo@bar.org</To>
        <From>foo@bar.org</From>
        <Server>smtp.bar.org</Server>
        <Port>465</Port>
        <Username>foo@bar.org</Username>
        <Password>fooo!</Password>
    </MailNotification>
</root>

I am using Java 7, the complete XML is longer, but it’s no really big file. I thought about using a Stax Pull Parser because it seemed easy, but there’s one thing where I am not sure if it is really a good way:

When coming to a MailNotification element, I could e.g. create a new instance of e.g. a mail class, I have no problem with that. But: What if I come e.g. to an To element? How do I know if it is really inside a MailNotification element and not directly below the root? In other words: What I am missing is a best practice for handling states like “now I am in a MailNotification” element.

Note: I know I could verify the XML first, but imagine it would be allowed to have a To element inside a MailNotification element and a To element as children of another, semantically different element – same problem: I somehow need to keep track of states / context to make sure I interpret the To element correctly.

Thanks for any hint!

  • 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-03T22:14:30+00:00Added an answer on June 3, 2026 at 10:14 pm

    StAX Stream Reader are the best* choice. Just use the Java stack to keep your state, like in this example. The constants are XMLStreamConstants.

    XMLStreamReader reader;
    
    void parseRoot() {
        reader.require(START_ELEMENT, null, "root");
    
        while (reader.nextTag() == START_ELEMENT) {
            switch (reader.getLocalName()) {
            case "MailNotification":
                MailNotification mail = parseMail();
                // do something with mail
                break;
            // more cases
            }
        }
    
        reader.require(END_ELEMENT, null, "root");
    }
    
    MailNotification parseMail() {
        reader.require(START_ELEMENT, null, "MailNotification");
        MailNotification mail = new MailNotification();
    
        while (reader.nextTag() == START_ELEMENT) {
            switch (reader.getLocalName()) {
            case "To":
                mail.setTo(parseString());
                break;
            // more cases
            }
        }
    
        reader.require(END_ELEMENT, null, "MailNotification");
        return mail;
    }
    
    String parseString() {
        String text = "";
        if (reader.next() == CHARACTERS) {
            text = reader.getText();
            reader.next();
        }
        return text;
    }
    

    (*) Just to clarify on the “best choice”, it depends on what you want to do.
    JAXB is very good if your XML directly maps to the objects you want to create.
    JDOM is useful if you want to navigate you XML in complex ways, eg, if you implement something like XPath; but for simple parsing its overkill. This is the approach that consumes most memory.
    SAX was the lightest and most efficient parser before StAX was around.

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

Sidebar

Related Questions

Wondering what others do / best practice for communicating between layers. This question relates
wondering how I can replace all special chars on my string like: hello this
Wondering what the best way to make this more efficient, perhaps with jQuery. I
Wondering if anyone out there has ran into this before.... I'd like to use
Wondering if anyone can help me with this annoying but trivial (in terms of
Wondering what is the best or most popular database client tool. Similar to Microsoft's
Wondering if anyone has a good solution for this. My app is displaying nothing
Wondering how does an application like Process Explorer or Combo Fix detect hidden process
Wondering if this is possible or something with this effect. public class MyModel {
Wondering if someone can tell me why this regular expression doesn't work. Expression ->

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.