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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:54:11+00:00 2026-05-16T14:54:11+00:00

I have a class that unmarshals xml from a 3rd party source (I have

  • 0

I have a class that unmarshals xml from a 3rd party source (I have no control over the content). Here is the snippet that unmarshals:

JAXBContext jContext = JAXBContext.newInstance("com.optimumlightpath.it.aspenoss.xsd"); 
Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
StringReader xmlStr = new StringReader(str.value);
Connections conns = (Connections) unmarshaller.unmarshal(xmlStr); 

Connections is a class generated dtd->xsd->class using xjc. The package com.optimumlightpath.it.aspenoss.xsd contains all such classes.

The xml I recieve contains a relative path in the DOCTYPE. Basically str.value above contains:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE Connections SYSTEM "./dtd/Connections.dtd">
<Connections>
...
</Connections>

This runs successfully as a java 1.5 application. In order to avoid the error above, I had to create a ./dtd directory off the project root and include all the dtd files (not sure why I had to do this but we’ll get to that).

I’ve since created a web service on Tomcat5.5 that uses the above class. I am getting [org.xml.sax.SAXParseException: Relative URI "./dtd/Connections.dtd"; can not be resolved without a document URI.] on the unmarshal line. I have tried creating ./dtd in every relavant folder (project root, WebContent, WEB-INF, tomcat working dir, etc) to no avail.

Question #1: Where can I locate ./dtd so that the class can find it when run as a tomcat webservice? Is there any tomcat or service config I need to do in order to get the directory recognized?

Question #2: Why does the class even need the dtd file in the first place? Doesn’t it have all the information it needs to unmarshal in the annotations of the dtd->xsd->class? I’ve read many posts about disabling validation, setting EntityResource, and other solutions, but this class isn’t always deployed as a web-service and I don’t want to have two code trains.

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

    When unmarshalling from an InputStream or Reader the parser does not know the systemId (uri / location) of the document, so it can not resolve relative paths. It seems the parser tries to resolve references using the current working directory, which only works when running from the ide or command line. In order to override this behaviour and do the resolving yourself you need to implement an EntityResolver, as Blaise Doughan mentioned.

    After some experimenting I found a standard way of doing this. You need to unmarshal from a SAXSource, which is in turn constructed from an XMLReader and an InputSource. In this example the dtd is located next to the annotated class and so can be found in the classpath.

    Main.java

    public class Main {
        private static final String FEATURE_NAMESPACES = "http://xml.org/sax/features/namespaces";
        private static final String FEATURE_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    
        public static void main(String[] args) throws JAXBException, IOException, SAXException {
            JAXBContext ctx = JAXBContext.newInstance(Root.class);
            Unmarshaller unmarshaller = ctx.createUnmarshaller();
    
            XMLReader xmlreader = XMLReaderFactory.createXMLReader();
            xmlreader.setFeature(FEATURE_NAMESPACES, true);
            xmlreader.setFeature(FEATURE_NAMESPACE_PREFIXES, true);
            xmlreader.setEntityResolver(new EntityResolver() {
                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    // TODO: Check if systemId really references root.dtd
                    return new InputSource(Root.class.getResourceAsStream("root.dtd"));
                }
            });
    
            String xml = "<!DOCTYPE root SYSTEM './root.dtd'><root><element>test</element></root>";
            InputSource input = new InputSource(new StringReader(xml));
            Source source = new SAXSource(xmlreader, input);
    
            Root root = (Root)unmarshaller.unmarshal(source);
            System.out.println(root.getElement());
        }
    }
    

    Root.java

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
        @XmlElement
        private String element;
    
        public String getElement() {
            return element;
        }
    
        public void setElement(String element) {
            this.element = element;
        }
    }
    

    root.dtd

    <?xml version="1.0" encoding="UTF-8"?>
    <!ELEMENT root (element)>
    <!ELEMENT element (#PCDATA)>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.