title: java.net.MalformedURLException - at parsing XML file by StAX
body: <p>I have to parse XML file with StAX. </p>
  
  <p>I caught bunch of exceptions:</p>
  
  <pre><code>javax.xml.stream.XMLStreamException: java.net.MalformedURLException
      at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.setInputSource(XMLStreamReaderImpl.java:217)
      at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.&lt;init&gt;(XMLStreamReaderImpl.java:189)
      at com.sun.xml.internal.stream.XMLInputFactoryImpl.getXMLStreamReaderImpl(XMLInputFactoryImpl.java:262)
      at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:129)
      at com.epam.lab.StaxXmlParser.&lt;init&gt;(StAXParserDemo.java:46)
      at com.epam.lab.StAXParserDemo.main(StAXParserDemo.java:20)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  Caused by: java.net.MalformedURLException
      at java.net.URL.&lt;init&gt;(URL.java:619)
      at java.net.URL.&lt;init&gt;(URL.java:482)
      at java.net.URL.&lt;init&gt;(URL.java:431)
      at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:610)
      at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1290)
      at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:1242)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:257)
      at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.setInputSource(XMLStreamReaderImpl.java:204)
  </code></pre>
  
  <p>here is how xml file loooks:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;staff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="newEmployee.xsd"&gt;    
      &lt;employee&gt;
          &lt;name&gt;Carl Cracker&lt;/name&gt;
          &lt;salary&gt;75000&lt;/salary&gt;
          &lt;hiredate year="1987" month="12" day="15" /&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;Harry Hacker&lt;/name&gt;
          &lt;salary&gt;50000&lt;/salary&gt;
          &lt;hiredate year="1989" month="10" day="1" /&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;Tony Tester&lt;/name&gt;
          &lt;salary&gt;40000&lt;/salary&gt;
          &lt;hiredate year="1990" month="3" day="15" /&gt;
      &lt;/employee&gt;    
  &lt;/staff&gt;
  </code></pre>
  
  <p>Here is code snippet:</p>
  
  <pre><code>public class StAXParserDemo {
      public static void main(String[] args) {
          try {
              StaxXmlParser staxXmlParser = new StaxXmlParser(EMPLOYEE_XML.getFilename());
              List&lt;Employee&gt; employees = staxXmlParser.parseEmployee();
              for (Employee emp : employees) {
                  System.out.println(emp);
              }
          } catch (Throwable e) {
              e.printStackTrace();
          }    
      }
  }
  
  class StaxXmlParser {
  
      private List&lt;Employee&gt; employeeList;
      private Employee currentEmployee;
      private String tagContent;
      private XMLStreamReader reader;
  
      public StaxXmlParser(String filename) {
          employeeList = null;
          currentEmployee = null;
          tagContent = null;
  
          try {
              XMLInputFactory factory = XMLInputFactory.newFactory();
              reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream(filename));
              parseEmployee();
          } catch (XMLStreamException e) {
              e.printStackTrace();
          }
      }
  
      public List&lt;Employee&gt; parseEmployee() throws XMLStreamException {
          while (reader.hasNext()) {
              int event = reader.next();
  
              switch (event) {
                  case XMLStreamConstants.START_ELEMENT:
                      if ("employee".equals(reader.getLocalName())) {
                          currentEmployee = new Employee();
                      }
                      if ("staff".equals(reader.getLocalName())) {
                          employeeList = new ArrayList&lt;&gt;();
                      }
                      if ("hireday".equals(reader.getLocalName())) {
                          int yearAttr = Integer.parseInt(reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "year"));
                          int monthAttr = Integer.parseInt(reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "month"));
                          int dayAttr = Integer.parseInt(reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "day"));
  
                          currentEmployee.setHireDay(yearAttr, monthAttr, dayAttr);
                      }
                      break;
  
                  case XMLStreamConstants.CHARACTERS:
                      tagContent = reader.getText().trim();
                      break;
  
                  case XMLStreamConstants.END_ELEMENT:
                      switch (reader.getLocalName()) {
                          case "employee":
                              employeeList.add(currentEmployee);
                              break;
                          case "name":
                              currentEmployee.setName(tagContent);
                              break;
                          case "salary":
                              currentEmployee.setSalary(Double.parseDouble(tagContent));
                              break;
                      }
              }
          }
          return employeeList;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Follow suggestions I changed to next:</p>
  
  <blockquote>
    <p>reader = factory.createXMLStreamReader(new FileInputStream(new File(filename)));</p>
  </blockquote>
  
  <p>And now it printed next output:</p>
  
  <pre><code>Employee { name=Carl Cracker, salary=75000.0, hireDay=null }
  Employee { name=Harry Hacker, salary=50000.0, hireDay=null }
  Employee { name=Tony Tester, salary=40000.0, hireDay=null }
  </code></pre>
  
  <p>What is wrong with attributes extracting? I changed question closer to new issue.</p>
  
  <p>Why does this happen? Any suggestions?</p>