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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:45:35+00:00 2026-05-10T13:45:35+00:00

I’m looking for the best method to parse various XML documents using a Java

  • 0

I’m looking for the best method to parse various XML documents using a Java application. I’m currently doing this with SAX and a custom content handler and it works great – zippy and stable.

I’ve decided to explore the option having the same program, that currently recieves a single format XML document, receive two additional XML document formats, with various XML element changes. I was hoping to just swap out the ContentHandler with an appropriate one based on the first ‘startElement’ in the document… but, uh-duh, the ContentHandler is set and then the document is parsed!

... constructor ... { SAXParserFactory spf = SAXParserFactory.newInstance();  try { SAXParser sp = spf.newSAXParser(); parser = sp.getXMLReader(); parser.setErrorHandler(new MyErrorHandler()); } catch (Exception e) {}   ... parse StringBuffer ... try { parser.setContentHandler(pP); parser.parse(new InputSource(new StringReader(xml.toString()))); return true; } catch (IOException e) {     e.printStackTrace(); } catch (SAXException e) {     e.printStackTrace(); } ... 

So, it doesn’t appear that I can do this in the way I initially thought I could.

That being said, am I looking at this entirely wrong? What is the best method to parse multiple, discrete XML documents with the same XML handling code? I tried to ask in a more general post earlier… but, I think I was being too vague. For speed and efficiency purposes I never really looked at DOM because these XML documents are fairly large and the system receives about 1200 every few minutes. It’s just a one way send of information

To make this question too long and add to my confusion; following is a mockup of some various XML documents that I would like to have a single SAX, StAX, or ?? parser cleanly deal with.

products.xml:

<products> <product>   <id>1</id>   <name>Foo</name> <product>   <id>2</id>   <name>bar</name> </product> </products> 

stores.xml:

<stores> <store>   <id>1</id>   <name>S1A</name>   <location>CA</location> </store> <store>   <id>2</id>   <name>A1S</name>   <location>NY</location> </store> </stores> 

managers.xml:

<managers> <manager>   <id>1</id>   <name>Fen</name>   <store>1</store> </manager> <manager>   <id>2</id>   <name>Diz</name>   <store>2</store> </manager> </managers> 
  • 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. 2026-05-10T13:45:35+00:00Added an answer on May 10, 2026 at 1:45 pm

    As I understand it, the problem is that you don’t know what format the document is prior to parsing. You could use a delegate pattern. I’m assuming you’re not validating against a DTD/XSD/etcetera and that it is OK for the DefaultHandler to have state.

    public class DelegatingHandler extends DefaultHandler {      private Map<String, DefaultHandler> saxHandlers;     private DefaultHandler delegate = null;      public DelegatingHandler(Map<String, DefaultHandler> delegates) {         saxHandlers = delegates;     }      @Override     public void startElement(String uri, String localName, String name,             Attributes attributes) throws SAXException {        if(delegate == null) {            delegate = saxHandlers.get(name);        }        delegate.startElement(uri, localName, name, attributes);     }      @Override     public void endElement(String uri, String localName, String name)             throws SAXException {         delegate.endElement(uri, localName, name);     }  //etcetera... 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • 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 Just tableData[row_index] = myData[row_index] May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer Have you debugged into some_func? Try to windbg that program.… May 11, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer your approach is the fastest ... if you think it's… May 11, 2026 at 11:42 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.