2014-07-20 90 views
0

當我要求獲取ID mehtod getIdFromDatabase從我的XML數據庫,但告訴我exception [Fatal Error] :1:1: Content is not allowed in prolog.的異常當我解析XML文件

調用方法getIdFromDatabase:在java中

nameLayer =getIdLayerFromDatabase("//Project/Layer[Name="+"'"+Name+"'"+"]/@idLayer","ProjectDataBase.xml"); 

方法:

public int getIdFromDatabase(String PathXPath, String Path) throws JAXBException { 
      int maxid = -1, id = -1; 
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
      try { 
       DocumentBuilder builder = domFactory.newDocumentBuilder(); 
       //***** exception here****************************** 
       Document dDoc = builder.parse(new InputSource(new ByteArrayInputStream(Path.getBytes("UTF-8")))); 
       XPath xPath = XPathFactory.newInstance().newXPath(); 
       NodeList nl = (NodeList) xPath.evaluate(PathXPath, dDoc, XPathConstants.NODESET); 
       for (int i = 0; i < nl.getLength(); i++) { 
        id = Integer.parseInt(nl.item(i).getNodeValue()); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return id; 
     } 

XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<Project xmlns="http://www.example.org/ProjectDataBase" name="شئؤ" location="Projects//شئؤ//ProjectDataBase.xml" CreationDate="2014-07-20"> 
    <Layer idLayer="0"> 
     <Name>طبقة 1</Name> 
    </Layer> 
</Project> 

回答

1

問題是您正在嘗試解析路徑而不是文件。更改行:

Document dDoc = builder.parse(new InputSource(new ByteArrayInputStream(Path.getBytes("UTF-8")))); 

由:

Document dDoc = builder.parse(new FileInputStream(Path)); 
+0

感謝的是我需要什麼。 – user3858035

相關問題