title: How to validate XML with schema URLs that return HTTP 301?
body: <p>I need to validate a XML file whose schema URLs (referenced at schemaLocation attribute) return an HTTP 301 error pointing out the correct URL. Moreover, those schemas have schema imports whose URLs return HTTP 301 pointing out the correct URL too.</p>
  
  <p>JDom2 seems unable to resolve the HTTP 301 errors like a simple browser does. Is there a way to force JDom2 to resolve properly such an error?. What JDom2 class/method i must override to do that?. Is there another java XML library that can do that?</p>
  
  <p>FYI: The erroneous URLs are like <a href="http://schema-url" rel="nofollow noreferrer">http://schema-url</a> and the HTTP 301 errors return URLs like </p>
  
  <p>----------------------- some progresses later ------------------------------</p>
  
  <p>A first approach to solve to this problem could be something like this:</p>
  
  <p><a href="https://stackoverflow.com/questions/24913206/jdom2-follow-redirects-http-error-301">JDOM2 - Follow Redirects (HTTP Error 301)</a></p>
  
  <p>i.e, using the folling snippet:</p>
  
  <pre><code>URL httpurl = new URL(.....);
  HTTPURLConnection conn = (HTTPUrlConnection)httpurl.openConnection();
  conn.setInstanceFollowRedirects(true);
  conn.connect();
  Document doc = saxBuilder.build(conn.getInputStream());
  </code></pre>
  
  <p>But in my case, the application reads the xml from a gzip xml file, so it will not work. I have decided use something like:</p>
  
  <pre><code>final SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
  builder.setEntityResolver(new RedirectEntityResolver());
  document = builder.build(isXml);
  </code></pre>
  
  <p>Where <code>isXml</code> is the decompressed XML file input stream and <code>RedirectEntityResolver</code> is coded like:</p>
  
  <pre><code>public class RedirectEntityResolver implements EntityResolver {
  
      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  
          URL httpurl = new URL(systemId);
          HttpURLConnection conn = (HttpURLConnection) httpurl.openConnection();
          conn.setInstanceFollowRedirects(true);
          conn.connect();
  
          return new InputSource(conn.getInputStream());
      }
  
  }
  </code></pre>
  
  <p>But it doesn't work. The <code>HttpURLConnection</code> seems unable to resolve the redirects and i get the same error:</p>
  
  <pre><code>org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'Document Moved'.
  </code></pre>