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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:32:25+00:00 2026-06-18T16:32:25+00:00

I’m attempting to use JAXB with data that does not technically fit the XML

  • 0

I’m attempting to use JAXB with data that does not technically fit the XML standard; in particular, the names of the elements are technically invalid as they begin with numeric characters. Here’s an overview of what the schema looks like.

<xs:element name = "ITEM">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="01" />
            <xs:element name="08" />
            <xs:element name="10">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="10_A" />
                        <xs:element name="10_B" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            ...
            ...Many more elements...
            ...
        </xs:sequence>
    </xs:complexType>
</xs:element>

Unfortunately, I don’t have the ability to modify this. Since the full ITEM is huge and has many levels of depth, using an automated tool like JAXB to create classes is a must. To do so, I prefixed the names of the elements with a character (in this case, ‘m’) so that XJC would accept it. I was hoping that at runtime, I could map the XML tags to my Java class in order to unmarshal the input into a Java object. In particular, something like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "m01",
    "m08",
    "m10",
    ...
})
@XmlRootElement(name = "ITEM")
public class ITEM {
    @XmlElement(name = "01")
    protected String m01;
    @XmlElement(name = "08")
    protected String m08;
    @XmlElement(name = "10")
    protected M10 m10;
    ...
}

M10 would look like:

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "m10a",
        "m10b",
        ...
})
public static class M10 {
    @XmlElement(name = "10_A")
    protected String m10a;
    @XmlElement(name = "10_B")
    protected String m10b;
    ...
}

I was hoping that JAXB would be able to match the @XmlElement tag to the tag in the input, but unfortunately this didn’t work out for me because JAXB won’t have any of this business with improper tags. If anybody is interested, the particular exception is:

org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup

Anyone have any advice on how to get around this problem? I feel like I could potentially run a regex swap on the input XML before JAXB parses it (and thus bypassing this issue completely), but modifying the input in such a way is rather undesirable.

  • 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-18T16:32:26+00:00Added an answer on June 18, 2026 at 4:32 pm

    It is not the JAXB (JSR-222) implementation complaining, but the underlying parser being used. The trick will be to find a tolerant XML parser.

    StAX

    If you can find a StAX (JSR-173) parser capable of handling this content then you could do the following:

    import java.io.StringReader;
    import javax.xml.bind.*;
    import javax.xml.stream.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(ITEM.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StringReader xml = new StringReader("<ITEM><01>Hello World</01></ITEM");
            XMLStreamReader xsr = XMLInputFactory.newFactory().createXMLStreamReader(xml);
            ITEM item = (ITEM) unmarshaller.unmarshal(xsr);
        }
    
    }
    

    SAX

    Or if you find a SAX parser then you can do the following:

    import java.io.StringReader;
    import javax.xml.bind.*;
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
    
            JAXBContext jc = JAXBContext.newInstance(ITEM.class);
            UnmarshallerHandler unmarshallerHandler = jc.createUnmarshaller().getUnmarshallerHandler();
            xr.setContentHandler(unmarshallerHandler);
    
            StringReader xml = new StringReader("<ITEM><01>Hello World</01></ITEM");
            InputSource inputSource = new InputSource(xml);
            xr.parse(inputSource);
    
            ITEM item = (ITEM) unmarshallerHandler.getResult();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

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.