title: How to unmarshal this type of xml in java
body: <p>I am new to Rest API. I have a xml output which i need to unmarshal. Below is the xml output:</p>
  
  <pre><code>&lt;dsml&gt;
      &lt;entries&gt;
          &lt;entry dn="uid=7686,c=in,ou=pages,o=example.com"&gt;
              &lt;att name="uid"&gt;
                  &lt;value&gt;7568766&lt;/value&gt;
              &lt;/att&gt;
              &lt;att name="email"&gt;
                  &lt;value&gt;new@gmail.com&lt;/value&gt;
              &lt;/att&gt;
              &lt;att name="callname"&gt;
                  &lt;value&gt;John&lt;/value&gt;
              &lt;/att&gt;
          &lt;/entry&gt;
          &lt;entry dn="uid=7689,c=in,ou=pages,o=example.com"&gt;
              &lt;att name="uid"&gt;
                  &lt;value&gt;7678766&lt;/value&gt;
              &lt;/att&gt;
              &lt;att name="callname"&gt;
                  &lt;value&gt;Mike&lt;/value&gt;
              &lt;/att&gt;
          &lt;/entry&gt;
          &lt;entry dn="uid=7690,c=in,ou=pages,o=example.com"&gt;
              &lt;att name="uid"&gt;
                  &lt;value&gt;75858766&lt;/value&gt;
              &lt;/att&gt;
              &lt;att name="email"&gt;
                  &lt;value&gt;old@gmail.com&lt;/value&gt;
              &lt;/att&gt;
              &lt;att name="callname"&gt;
                  &lt;value&gt;rahul&lt;/value&gt;
              &lt;/att&gt;
          &lt;/entry&gt;
      &lt;/entries&gt;
  &lt;/dsml&gt;
  </code></pre>
  
  <p>The actual xml output has total 37 entries. Below is the model class: </p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlRootElement(name = "user")
  public class User implements Serializable{
      private static final long serialVersionUID = 1L;
  
      @XmlElement(name = "uid")
      private int uid;
  
      @XmlElement(name = "callname")
      private String callname;
  
      @XmlElement(name = "email")
      private String email;
  
      public int getId() {
          return uid;
      }
      public void setId(int uid) {
          this.uid = uid;
      }
      public String getFirstName() {
          return callname;
      }
      public void setFirstName(String callname) {
          this.callname = callname;
      }
      public String getmail() {
          return email;
      }
      public void setmail(String email) {
          this.email = email;
      }
  }
  </code></pre>
  
  <p>Below is the Rest Api class code:</p>
  
  <pre><code>public class UsingRestAPI {
      public static void main(String[] args) 
      {
          try
          {
              URL url = new URL("www.example.com");
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setRequestMethod("GET");
              conn.setRequestProperty("Accept", "application/xml");
  
              if (conn.getResponseCode() != 200) 
              {
                  throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
              }
  
              BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
              String apioutput="",temp="";
              while ((apioutput = br.readLine()) != null) {
                  temp += apioutput;
                  System.out.println(apioutput);
              }
  
  
              JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
              User user = (User) jaxbUnmarshaller.unmarshal(new StringReader(temp));
  
              System.out.println(user.getId());
              System.out.println(user.getFirstName());
              System.out.println(user.getmail());
  
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JAXBException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>While executing this code, i am getting error </p>
  
  <p>javax.xml.bind.UnmarshalException
   - with linked exception:
  [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 110; External DTD: Failed to read external DTD 'dsml.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property.]</p>
  
  <p>I have tried by changing the propert ACCESS_EXTERNAL_DTD to true, but it gave another error:</p>
  
  <pre><code>javax.xml.bind.PropertyException: name: http://javax.xml.XMLConstants/property/accessExternalDTD value: true
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.setProperty(Unknown Source)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.setProperty(Unknown Source)
      at UsingRestAPI.main(UsingRestAPI.java:60)
  </code></pre>