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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:00:12+00:00 2026-05-27T14:00:12+00:00

I have used JAXB to create a class for the following schema (used in

  • 0

I have used JAXB to create a class for the following schema (used in a webservice):

<xs:complexType name="ExceptionType">
        <xs:attribute name="errorCode" type="xs:positiveInteger" use="required"/>
        <xs:attribute name="outcomeType" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:token">
                    <xs:enumeration value="rejectFile"/>
                    <xs:enumeration value="rejectSubmission"/>
                    <xs:enumeration value="continue"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
</xs:complexType>

Though the actual XML they will send is

<Exception errorCode="1503"outcomeType="continue">

(with no space with “1503” and outcomeType).

Right now, I’m replacing <Exception errorCode="(\d*)"outcomeType with <Exception errorCode="\1" outcomeType in the whole XML response before feeding it to JAXB unmarshaller and it works, but I wonder if some other XML responses will have this “bug”.
Is there an easier way have JAXB accept XML tags with this attr1="value"attr2 bug? Or maybe using some custom XMLFilterImpl?

  • 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-27T14:00:13+00:00Added an answer on May 27, 2026 at 2:00 pm

    Is there an easier way have JAXB accept XML tags with this attr1=”value”attr2 bug?

    No, because this isn’t a bug.

    XML containing attr1="value"attr2 aren’t well-formed, thus JAXB cannot parse it and will throw an exception indicating a fatal, non-recoverable error.

    If you expect XML-ish data of this kind and you have no control over it (you receive it from a third party), then your solution seems OK. However, if I was you I would contact this third party and tell them that they’re spouting out invalid XML and that isn’t too professional.

    An alternative to replacing strings with regular expressions could be something like this (but this isn’t exactly easy):

    public String toWellFormed(String xml) throws IOException, SAXException {             
      StringBuilder sb = new StringBuilder(xml);                                                 
    
      XMLReader reader = XMLReaderFactory.createXMLReader();                                     
      reader.setContentHandler(null);                                                            
      reader.setErrorHandler(null);                                                              
    
      boolean threw = true;                                                                      
      while (threw)                                                                              
        try {                                                                                    
          reader.parse(new InputSource(new StringReader(sb.toString())));                        
          threw = false;                                                                         
        } catch (SAXParseException ex) {                                                         
          if (ex.getMessage().contains("must be followed by either attribute specifications")) {
            threw = true;
            int line = ex.getLineNumber();                                                       
            int column = ex.getColumnNumber();                                                   
            sb.insert(line * column - 1, ' ');                                                                                                            
          } else                                                                                 
            throw ex;                                                                            
        }                                                                                        
    
      return sb.toString();                                                                      
    }                       
    
    String malformedXml = "<test a='a'b='b'c='c'/>";  
    String wellFormedXml = toWellFormed(malformedXml);
    "<test a='a'b='b'c='c'/>".equals(wellFormedXml);
    

    JAXB’s Unmarshaller should be able to handle wellFormedXml after the process.

    If replacing stuff with regular expressions is good enough, because your data doesn’t contain too much stuff to search for and contains only the particular formatting error you’ve described, then don’t use my solution of course, but if you expect more formatting error you could use something like this.

    Notice, that I explicitly set the reader’s error and content handler to null. This is because given a malformed XML they’re never called; the reader will fail early, because this is a fatal, non-recoverable error. This is of course very bad for us, because if the document contains 10 errors like you’ve described, then my method parses the XML 10 times, until it founds every error. I’m not aware of an XML parser in the JDK, that would report formatting errors and continue parsing (reporting every error during the process).

    Using a proper ErrorHandler you could handle warnings and errors gracefully, however fatal errors could not be handled even with an ErrorHandler (after its fatalError method gets called, processing stops).

    Using an XMLFilter implementation wouldn’t help you either, because if you simply use the default XMLFilterImpl class that forwards all of its calls to a delegate XMLReader then you would face the same problem as before: on the first error, processing stops. As a matter of fact, if you want to implement something, then implement the XMLReader interface directly (XMLFilter only adds the setParent and getParent method—bad design if you ask me). But implementing an XMLReader that can parse malformed XML is probably going to be tedious.

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

Sidebar

Related Questions

I have used a custom class to implement shared resource functionality in my WPF
I have an existing XML schema that contains a type that I would like
I have a JAXB class like this: public class Game { private Date startTime;
I have used an expander control to create some sort of slide-out panel which
We have an xsd schema with a declaration like this: <xsd:simpleType name=customId> <xsd:annotation> <xsd:appinfo>
I have the following class: public class Message { private String text; public String
I have used the silverlight control in CRM 2011.It also published on form but
We have used Shibboleth to authenticate users. It works great. The issue is that
I have used some jquery components in my web site, Suddenly i'm getting an
I have used a plugin that uses prototype js, it's working fine but the

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.