Trying to parse a xml but getting malformedURLexception - java

I´m trying to parse a xml but getting a error.
public static String xmlVersion(String source) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(source);
return document.getDocumentElement().getAttribute("versao");
} catch (SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();
}
return null;
}
And the stackTrace
java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?><nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe"><NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe versao="2.00" Id="NFe31130605633253000205550010000002571000169370"><ide><cUF>31</cUF><cNF>00016937</cNF><natOp>Devolucao de compra para comercializacao em operacao com mer</natOp><indPag>0</indPag><mod>55</mod><serie>1</serie><nNF>257</nNF><dEmi>2013-06-14</dEmi><dSaiEnt>2013-06-14</dSaiEnt><tpNF>1</tpNF><cMunFG>3147907</cMunFG><tpImp>1</tpImp><tpEmis>1</tpEmis><cDV>0</cDV><tpAmb>1</tpAmb><finNFe>1</finNFe><procEmi>0</procEmi><verProc>2.0.1.2</verProc></ide><emit><CNPJ>05633253000205</CNPJ><xNome>SUPERMERCADO EXPOSICAO LTDA</xNome><xFant>SUPERMERCADO EXPOSICAO 2</xFant><enderEmit><xLgr>RUA JOAO TEIXEIRA MENDES</xLgr><nro>541</nro><xBairro>N.S.APARECIDA</xBairro><cMun>3147907</cMun><xMun>PASSOS</xMun><UF>MG</UF><CEP>37902419</CEP><cPais>1058</cPais><xPais>BRASIL</xPais><fone>3535215796</fone></enderEmit><IE>4792308680128</IE><CRT>3</CRT></emit><dest><CNPJ>16849231000619</CNPJ><xNome>ITAMBE ALIMENTOS S A</xNome><enderDest><xLgr>RODOVIA BR 262 ANEXO I KM403</xLgr><nro>S/N</nro><xBairro>PATAFUFO</xBairro><cMun>3147105</cMun><xMun>Para de Minas</xMun><UF>MG</UF><CEP>35661390</CEP><cPais>1058</cPais><xPais>BRASIL</xPais></enderDest><IE>0020322880432</IE></dest><det nItem="1"><prod><cProd>13169</cProd><cEAN>7896051111832</cEAN><xProd>BEBIDA LACTEA ITAMBE FAMILIA VITAMINA C/ FRUTAS 1L</xProd><NCM>04039000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>1.0000</qCom><vUnCom>3.1900000000</vUnCom><vProd>3.19</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>1.0000</qTrib><vUnTrib>3.1900000000</vUnTrib><vOutro>0.07</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>1.24</vBC><pICMS>18.00</pICMS><vICMS>0.22</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="2"><prod><cProd>23003</cProd><cEAN>7896051121831</cEAN><xProd>COALHADA ITAMBE DESNATADA 130GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>3.0000</qCom><vUnCom>0.8300000000</vUnCom><vProd>2.49</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>3.0000</qTrib><vUnTrib>0.8300000000</vUnTrib><vOutro>0.05</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>33.33</pRedBC><vBC>1.66</vBC><pICMS>18.00</pICMS><vICMS>0.30</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="3"><prod><cProd>23004</cProd><cEAN>7896051125037</cEAN><xProd>IOGURTE FRUTAMBE BAUNILHA AMEIXA 130GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>7.0000</qCom><vUnCom>0.9800000000</vUnCom><vProd>6.86</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>7.0000</qTrib><vUnTrib>0.9800000000</vUnTrib><vOutro>0.15</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>2.67</vBC><pICMS>18.00</pICMS><vICMS>0.48</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="4"><prod><cProd>23005</cProd><cEAN>7896051125259</cEAN><xProd>IOGURTE FRUTAMBE BAUNILHA MARACUJA 130GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>9.0000</qCom><vUnCom>0.9500000000</vUnCom><vProd>8.55</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>9.0000</qTrib><vUnTrib>0.9500000000</vUnTrib><vOutro>0.18</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>3.33</vBC><pICMS>18.00</pICMS><vICMS>0.60</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="5"><prod><cProd>23010</cProd><cEAN>7896051127093</cEAN><xProd>IOGURTE ITAMBE GOODY FRUTI 600GR</xProd><NCM>04039000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>5.0000</qCom><vUnCom>1.7500000000</vUnCom><vProd>8.75</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>5.0000</qTrib><vUnTrib>1.7500000000</vUnTrib><vOutro>0.19</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>3.40</vBC><pICMS>18.00</pICMS><vICMS>0.61</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="6"><prod><cProd>23011</cProd><cEAN>7896051121268</cEAN><xProd>IOGURTE ITAMBE NATURAL C/MEL 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>7.0000</qCom><vUnCom>0.8900000000</vUnCom><vProd>6.23</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>7.0000</qTrib><vUnTrib>0.8900000000</vUnTrib><vOutro>0.13</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>2.42</vBC><pICMS>18.00</pICMS><vICMS>0.44</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="7"><prod><cProd>23012</cProd><cEAN>7896051121282</cEAN><xProd>IOGURTE ITAMBE NATURAL DESNATADO 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>2.0000</qCom><vUnCom>0.8900000000</vUnCom><vProd>1.78</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>2.0000</qTrib><vUnTrib>0.8900000000</vUnTrib><vOutro>0.04</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>0.69</vBC><pICMS>18.00</pICMS><vICMS>0.12</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="8"><prod><cProd>23014</cProd><cEAN>7896051121251</cEAN><xProd>IOGURTE ITAMBE NATURAL INTEGRAL 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>7.0000</qCom><vUnCom>0.8900000000</vUnCom><vProd>6.23</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>7.0000</qTrib><vUnTrib>0.8900000000</vUnTrib><vOutro>0.13</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>2.42</vBC><pICMS>18.00</pICMS><vICMS>0.44</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="9"><prod><cProd>23014</cProd><cEAN>7896051121251</cEAN><xProd>IOGURTE ITAMBE NATURAL INTEGRAL 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>8.0000</qCom><vUnCom>1.2500000000</vUnCom><vProd>10.00</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>8.0000</qTrib><vUnTrib>1.2500000000</vUnTrib><vOutro>0.21</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>3.89</vBC><pICMS>18.00</pICMS><vICMS>0.70</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="10"><prod><cProd>23017</cProd><cEAN>7896051142249</cEAN><xProd>IOGURTE ITAMBEZINHO FRUTAS 360GR</xProd><NCM>40610900</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>5.0000</qCom><vUnCom>2.0900000000</vUnCom><vProd>10.45</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>5.0000</qTrib><vUnTrib>2.0900000000</vUnTrib><vOutro>0.22</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>4.06</vBC><pICMS>18.00</pICMS><vICMS>0.73</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="11"><prod><cProd>23018</cProd><cEAN>7896051142218</cEAN><xProd>IOGURTE ITAMBEZINHO MORANGO 360GR</xProd><NCM>04061090</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>2.0000</qCom><vUnCom>1.9600000000</vUnCom><vProd>3.92</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>2.0000</qTrib><vUnTrib>1.9600000000</vUnTrib><vOutro>0.08</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>1.52</vBC><pICMS>18.00</pICMS><vICMS>0.27</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="12"><prod><cProd>23020</cProd><cEAN>7896051123842</cEAN><xProd>IOGURTE VITAMBE COCO 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>15.0000</qCom><vUnCom>0.8500000000</vUnCom><vProd>12.75</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>15.0000</qTrib><vUnTrib>0.8500000000</vUnTrib><vOutro>0.27</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>4.96</vBC><pICMS>18.00</pICMS><vICMS>0.89</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="13"><prod><cProd>23021</cProd><cEAN>7896051123859</cEAN><xProd>IOGURTE VITAMBE FRUTAS VERMELHAS 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>1.0000</qCom><vUnCom>0.8500000000</vUnCom><vProd>0.85</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>1.0000</qTrib><vUnTrib>0.8500000000</vUnTrib><vOutro>0.02</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>0.33</vBC><pICMS>18.00</pICMS><vICMS>0.06</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="14"><prod><cProd>23023</cProd><cEAN>7896051123828</cEAN><xProd>IOGURTE VITAMBE MARACUJA 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>2.0000</qCom><vUnCom>0.8500000000</vUnCom><vProd>1.70</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>2.0000</qTrib><vUnTrib>0.8500000000</vUnTrib><vOutro>0.04</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>0.66</vBC><pICMS>18.00</pICMS><vICMS>0.12</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="15"><prod><cProd>23024</cProd><cEAN>7896051123811</cEAN><xProd>IOGUTE VITAMBE MORANGO 180GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>11.0000</qCom><vUnCom>0.8500000000</vUnCom><vProd>9.35</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>11.0000</qTrib><vUnTrib>0.8500000000</vUnTrib><vOutro>0.20</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>3.64</vBC><pICMS>18.00</pICMS><vICMS>0.66</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="16"><prod><cProd>23027</cProd><cEAN>7896051126010</cEAN><xProd>LEITE FERMENTADO ITAMBE 80GR</xProd><NCM>40390000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>4.0000</qCom><vUnCom>0.4100000000</vUnCom><vProd>1.64</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>4.0000</qTrib><vUnTrib>0.4100000000</vUnTrib><vOutro>0.04</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>0.64</vBC><pICMS>18.00</pICMS><vICMS>0.12</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><det nItem="17"><prod><cProd>23068</cProd><cEAN>7896051121817</cEAN><xProd>COALHADA ITAMBE NATURAL 130GR</xProd><NCM>40310000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>1.0000</qCom><vUnCom>0.8300000000</vUnCom><vProd>0.83</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>1.0000</qTrib><vUnTrib>0.8300000000</vUnTrib><vOutro>0.02</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>33.33</pRedBC><vBC>0.55</vBC><pICMS>18.00</pICMS><vICMS>0.10</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="18"><prod><cProd>23384</cProd><cEAN>7896051125525</cEAN><xProd>IOGURTE ITAMBE FIT MORANGO MACA AMEIXA 100GR</xProd><NCM>04031000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>1.0000</qCom><vUnCom>0.4000000000</vUnCom><vProd>0.40</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>1.0000</qTrib><vUnTrib>0.4000000000</vUnTrib><vOutro>0.01</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>0.16</vBC><pICMS>18.00</pICMS><vICMS>0.03</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISAliq><CST>01</CST><vBC>0.00</vBC><pPIS>0.00</pPIS><vPIS>0.00</vPIS></PISAliq></PIS><COFINS><COFINSAliq><CST>01</CST><vBC>0.00</vBC><pCOFINS>0.00</pCOFINS><vCOFINS>0.00</vCOFINS></COFINSAliq></COFINS></imposto></det><det nItem="19"><prod><cProd>24367</cProd><cEAN>7896051161011</cEAN><xProd>IOGURTE ITAMBEZINHO MORANGO 180GR</xProd><NCM>04031000</NCM><CFOP>5411</CFOP><uCom>UN</uCom><qCom>18.0000</qCom><vUnCom>0.8500000000</vUnCom><vProd>15.30</vProd><cEANTrib/><uTrib>UN</uTrib><qTrib>18.0000</qTrib><vUnTrib>0.8500000000</vUnTrib><vOutro>0.34</vOutro><indTot>1</indTot></prod><imposto><ICMS><ICMS70><orig>0</orig><CST>70</CST><modBC>2</modBC><pRedBC>61.11</pRedBC><vBC>5.95</vBC><pICMS>18.00</pICMS><vICMS>1.07</vICMS><modBCST>0</modBCST><vBCST>0.00</vBCST><pICMSST>0.00</pICMSST><vICMSST>0.00</vICMSST></ICMS70></ICMS><PIS><PISNT><CST>06</CST></PISNT></PIS><COFINS><COFINSNT><CST>06</CST></COFINSNT></COFINS></imposto></det><total><ICMSTot><vBC>44.19</vBC><vICMS>7.96</vICMS><vBCST>0.00</vBCST><vST>0.00</vST><vProd>111.27</vProd><vFrete>0.00</vFrete><vSeg>0.00</vSeg><vDesc>0.00</vDesc><vII>0.00</vII><vIPI>0.00</vIPI><vPIS>0.00</vPIS><vCOFINS>0.00</vCOFINS><vOutro>2.39</vOutro><vNF>113.66</vNF></ICMSTot></total><transp><modFrete>9</modFrete></transp><infAdic><infCpl>BC ICMS ST 57,45 2-3-4-5-6-7-8-9-17-18- 1-10-11-12-13-14-15-16-19-</infCpl></infAdic></infNFe><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI="#NFe31130605633253000205550010000002571000169370"><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>9WsoFBTh8atNnEZJn45m83jRBpM=</DigestValue></Reference></SignedInfo><SignatureValue>UKpRdw1Sv+lMvkKdplOckAqlnWcjIh1R7Ymc44sIfFL8VkuKnDhBr5gj5VV4Q1ItHNjY7hjU7GadHW25iCAyfYnRQULUq4zDuYcc4xwfZ/iU1PMeeeH3QfkE9oDODm+jS6kldX9wPGnzOP3eRukk57RMcrfnyjofGHigGaZyWumZkMMYATG8ZBVmtLsBLtFhH66nBtaCBcBFGoZ9bDNOSmFaVeM/Gi5sJaBivImRXcXB0KrA2JSakqUxmNT/UwGasolrpclRp6UPvmqu44g/c2t91Fu2CqzN8nU+dKYBpO5UEY+PSBSVmA9YkBLMvFY49Fa8+95MgTKFicqs60bNoQ==</SignatureValue><KeyInfo><X509Data><X509Certificate>MIIILTCCBhWgAwIBAgIQJ9/fNxNJaVAK40MVZ+dGRTANBgkqhkiG9w0BAQsFADB1MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDE2MDQGA1UECxMtU2VjcmV0YXJpYSBkYSBSZWNlaXRhIEZlZGVyYWwgZG8gQnJhc2lsIC0gUkZCMRkwFwYDVQQDExBBQyBTSU5DT1IgUkZCIEc0MB4XDTEyMDYyMjAwMDAwMFoXDTE1MDYyMTIzNTk1OVowgesxCzAJBgNVBAYTAkJSMRMwEQYDVQQKFApJQ1AtQnJhc2lsMQswCQYDVQQIEwJNRzEPMA0GA1UEBxQGUEFTU09TMTYwNAYDVQQLFC1TZWNyZXRhcmlhIGRhIFJlY2VpdGEgRmVkZXJhbCBkbyBCcmFzaWwgLSBSRkIxFjAUBgNVBAsUDVJGQiBlLUNOUEogQTMxJDAiBgNVBAsUG0F1dGVudGljYWRvIHBvciBBUiBBUk1BQ1NFRzEzMDEGA1UEAxMqU1VQRVJNRVJDQURPIEVYUE9TSUNBTyBMVERBOjA1NjMzMjUzMDAwMjA1MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArZ0Wo7DYw+amjLN2TjKgU6ZH92oYC9xOpyWA6AyO1c9lLOyTOdaRFu1gIQPAeuEIhCtOpTHWXFM1FjhDtFeMRslPvIeaIIqYLlKkAYEaCprPo3vnnZA9F47Pmiq+70f6TrKI17xI1C3B24vY4aKO2BuOOq+SIl4CESIRL2b+oHUDi1NJs2KU3mMIXFu2VPrutfk3jh+JameeUomhhYkGI+EG+Vd/p+EUWRGZLog9sEz6SH1XsnT67GW7YX51IENmUFTX8rpFI7ohEZZwhL1yuqbJnBc+VlvBv3dNHhRLJNzqisFUhcK0g3eIvlYq958qpLFF3C/RUg4alYsrbMBXZQIDAQABo4IDQDCCAzwwgbgGA1UdEQSBsDCBraA4BgVgTAEDBKAvBC0xOTExMTk3MDc3NDE5NTUzNjUzMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDCgHgYFYEwBAwKgFQQTVEFDSU8gQk9SR0VTIEFWRUxBUqAZBgVgTAEDA6AQBA4wNTYzMzI1MzAwMDIwNaAXBgVgTAEDB6AOBAwwMDAwMDAwMDAwMDCBHXNhY0BldmVyZXN0aW5mb3JtYXRpY2EuY29tLmJyMAkGA1UdEwQCMAAwHwYDVR0jBBgwFoAUKcOB2ehusr9RJA+XRhMMuN7ENZgwDgYDVR0PAQH/BAQDAgXgMHgGA1UdIARxMG8wbQYGYEwBAgMZMGMwYQYIKwYBBQUHAgEWVWh0dHA6Ly9pY3AtYnJhc2lsLmFjc2luY29yLmNvbS5ici9yZXBvc2l0b3Jpby9kcGMvQUNfU0lOQ09SX1JGQi9EUENfQUNfU0lOQ09SX1JGQi5wZGYwggENBgNVHR8EggEEMIIBADBUoFKgUIZOaHR0cDovL2ljcC1icmFzaWwuY2VydGlzaWduLmNvbS5ici9yZXBvc2l0b3Jpby9sY3IvQUNTSU5DT1JSRkJHNC9MYXRlc3RDUkwuY3JsMFOgUaBPhk1odHRwOi8vaWNwLWJyYXNpbC5vdXRyYWxjci5jb20uYnIvcmVwb3NpdG9yaW8vbGNyL0FDU0lOQ09SUkZCRzQvTGF0ZXN0Q1JMLmNybDBToFGgT4ZNaHR0cDovL3JlcG9zaXRvcmlvLmljcGJyYXNpbC5nb3YuYnIvbGNyL0NlcnRpc2lnbi9BQ1NJTkNPUlJGQkc0L0xhdGVzdENSTC5jcmwwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMEMIGYBggrBgEFBQcBAQSBizCBiDBcBggrBgEFBQcwAoZQaHR0cDovL2ljcC1icmFzaWwuY2VydGlzaWduLmNvbS5ici9yZXBvc2l0b3Jpby9jZXJ0aWZpY2Fkb3MvQUNfU0lOQ09SX1JGQl9HNC5wN2MwKAYIKwYBBQUHMAGGHGh0dHA6Ly9vY3NwLmNlcnRpc2lnbi5jb20uYnIwDQYJKoZIhvcNAQELBQADggIBAFbKV1EtWcvZmXNZQTSCQVO9R0ix/gPdxAueT9+A5hfrHOfvHRyTjwU7TlcQfDIJPncMu5S1HFkD+C3S/APGDnwP/oCZs1MzmVekQNPfuTBNPNAIamdlRMxNdlDPG4r5H95Xi0GaXyU21WVyAmIVIWcenfd25HCNvDone+9M16tIltU5X83AHeGuy3P1EwxMrFrtgG6uHj4V13JI5usjpR3294NoRsuVhzRllED41ageeC7+J9GuMpbTnQGFQ6seEiUu5Ev2Lgrrf0MH9Eruovr7w69nGmEXCfZf5Cr/iVXJKA84KoG3sET89e4okowHek0xe9PqCBYpfnsgeqV/58PfQi6HqxunGB3hmB+kZ9qwjiITJq9MJ+PSVTaq3QJswekucT6vq7R3GCKDWVLFT3HWXeTgiV5shM/rbxkRLY+D+nvcM1DQOyXsZydX7MNURSVVXkhyxS6IPCZu5xUUsSC2OZprJLi1jjWcAlhCztlspo9bTok2vmC9mHhYf3nV9EC6DNUsH2dVZ58+z48pN+NzrX/MyRa1fINmV6WdF15+GPPEEhycRbwYejmUtkz6sEUhUEZR6fS/C0GXROoM0aVDiV9ImZVVaHrz/6majn6ygGjIF3pYthVLClLjvo3gx/uxy0vtT9vXNZOyHTrlnu0e6skumHxVd+KmyVrsP+1c</X509Certificate></X509Data></KeyInfo></Signature></NFe><protNFe versao="2.00"><infProt><tpAmb>1</tpAmb><verAplic>13_0_89</verAplic><chNFe>31130605633253000205550010000002571000169370</chNFe><dhRecbto>2013-06-14T09:01:24</dhRecbto><nProt>131131126789086</nProt><digVal>9WsoFBTh8atNnEZJn45m83jRBpM=</digVal><cStat>100</cStat><xMotivo>Autorizado o uso da NF-e</xMotivo></infProt></protNFe></nfeProc>
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.nfehost.util.XmlUtil.xmlVersion(XmlUtil.java:34)
at com.nfehost.service.processadora.Processadora.main(Processadora.java:13)
I want to pass a string but I´m getting a error, if a pass a file, everything works.
How can I parse a String source from a xml file?
My xml have a namespace

The method DocumentBuilder#parse(String) that you are using expects the URI of an XML source (e.g. file or HTTP URI), that holds the XML content, not the XML content itself.
If you really want to pass your XML content as a String, consider passing an InputSource wrapped around a StringReader to DocumentBuilder#parse(InputSource):
documentBuilder.parse(new InputSource(new StringReader(source)));

Related

Xpath - evaluate function returns javax.xml.transform.TransformerException

This is the first time I am using XPath, and I am trying to query a very basic example.
String xml = "<resp><status>good</status><msg>hi</msg></resp>";
XPathFactory xpathFactory = XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = xpathFactory.newXPath();
InputSource source = new InputSource(new StringReader(xml));
String status = "";
try {
status = (String) xpath.evaluate("//resp/status", source,XPathConstants.STRING);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("status=" + status);
I receive the following exception:
javax.xml.transform.TransformerException: Unable to evaluate expression using this context at
com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source) at
com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown
Source) at Heandlers.HTTP.x(HTTP.java:74) at
Views.Main.main(Main.java:15) Caused by: java.lang.RuntimeException:
Unable to evaluate expression using this context at
com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown
Source) at
com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown
Source) ... 5 more
--------- java.lang.RuntimeException: Unable to evaluate expression using this context at
com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown
Source) at
com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown
Source) at com.sun.org.apache.xpath.internal.XPath.execute(Unknown
Source) at
com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown
Source) at Heandlers.HTTP.x(HTTP.java:74) at
Views.Main.main(Main.java:15)
--------------- linked to ------------------ javax.xml.xpath.XPathExpressionException:
javax.xml.transform.TransformerException: Unable to evaluate
expression using this context at
com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown
Source) at Heandlers.HTTP.x(HTTP.java:74) at
Views.Main.main(Main.java:15) Caused by:
javax.xml.transform.TransformerException: Unable to evaluate
expression using this context at
com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source) at
com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source)
... 3 more Caused by: java.lang.RuntimeException: Unable to evaluate
expression using this context at
com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown
Source) at
com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown
Source) ... 5 more status=
Any suggestions?
Sounds like you're using an invalid InputSource, e.g. org.w3c.css.sac.InputSource. Make sure you've imported org.xml.sax.InputSource.

NullPointerException when accessing 2D array

First, here is my function "setProduct" :
public void setProduct(String code, int qty,double price,int nbrProduct){
System.out.println("Code = "+code);
System.out.println("qty = "+qty);
System.out.println("price = "+price);
System.out.println("nbrProduct = "+nbrProduct);
this.Produit[nbrProduct][0] = code;
this.Produit[nbrProduct][1] = Integer.toString(qty);
this.Produit[nbrProduct][2] = price+" €";
And now where I call :
Commande_Final Commande = new Commande_Final();
Commande = Cmd;
String name = Commande.getName();
double prixUnit = Function.GetMagPrice(radar,Qtyradar)/Qtyradar;
System.out.println("j'affiche le radar : "+name);
System.out.println("j'affiche le Qtyradar : "+Qtyradar);
System.out.println("j'affiche le prix du radar : "+prixUnit+" €");
System.out.println("nbr de produit = "+nbr_de_Produit);
Commande.setProduit(name,Qtyradar,prixUnit,nbr_de_Produit);
My problem :
I get an error when I do in "setProduct" => this.Produit[nbrProduct][0] = code;
The error says "java.lang.NullPointerException", I guess its trying to put an empty value in "this.Produit[nbrProduct][0]" but the thing is when I do the "System.out.println("Code = "+code);" its show me the correct code and the same for all others attributs :/
EDIT :
Here is how I create my Produit attribut :
public String[][] Produit = new String[99][3];
EDIT 2 :
stack trace :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Commande_Final.setProduit(Commande_Final.java:33)
at ZDialogInfo.Confirmer(ZDialogInfo.java:449)
at ZDialogV2$29.actionPerformed(ZDialogV2.java:1037)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The code (sorry I leave in french, then you see the real name I use):
public void Confirmer(boolean LoginCheck,String Nom,ZDialogInfo zInfo,final String Langue, Commande_Final Cmd) throws IOException{
Commande_Final Commande = Cmd;
...
if (Qtyradar > 0){
/*
* PROBLEME ICI
*
* */
String name = Commande.showName();
double prixUnit = Function.GetMagPrice(radar,Qtyradar)/Qtyradar;
System.out.println("j'affiche le radar : "+name);
System.out.println("j'affiche le Qtyradar : "+Qtyradar);
System.out.println("j'affiche le prix du radar : "+prixUnit+" €");
System.out.println("nbr de produit = "+nbr_de_Produit);
/*ZDialogInfo.Confirmer(ZDialogInfo.java:449) => */
Commande.setProduit(name,Qtyradar,prixUnit,nbr_de_Produit);
nbr_de_Produit++;
System.out.println("nbr de produit = "+nbr_de_Produit);
JP_CNom.add(JL_CRadar);
JP_CNom.add(JL_CQtyRadar);
JP_CNom.add(JL_CPrixRadar);
if (RnbrItem >= 1) JP_CNom.add(RCompo1);
if (RnbrItem >= 2) JP_CNom.add(RCompo2);
if (RnbrItem >= 3) JP_CNom.add(RCompo3);
}
My public class Commande_Final
public class Commande_Final {
private String[][] Produit = new String[MAX_Produit][3];
/*
*
* [Nbr de Produit][0] = Code_Item
* [Nbr de Produit][1] = Qty_Cmdé
* [Nbr de Produit][2] = Prix_Unitaire
*
* */
public void setProduit(String code, int qty,double prix,int nbrProduit){
System.out.println(" ------------------- Dans setProduit ---------------------- ");
System.out.println("Code = "+code);
System.out.println("qty = "+qty);
System.out.println("prix = "+prix);
System.out.println("nbrProduit = "+nbrProduit);
/*at Commande_Final.setProduit(Commande_Final.java:33) =>*/ this.Produit[nbrProduit][0] = code;
this.Produit[nbrProduit][1] = Integer.toString(qty);
this.Produit[nbrProduit][2] = prix+"";
}
Most probably you haven't set up the array correctly.
A simplest example:
Foo[] foo = new Foo[10];
it is just making an array with 10 reference to Foo, however there is no actual Foo object those reference is pointing to. Hence it will cause NPE when you are trying to access foo[0].bar()
Similar,
Product[][] product = new Product[10][];
is going to give u an array of 10 reference pointing to Product[]. However there is no actual Product array object created. Therefore similar to the above example, you will get NPE if you do product[1][0], because product[1] is point to null, NPE is thrown because you want to access [0] of that null Product[] reference. You need to instantiate them explicitly.
Update:
With the extra code that OP quoted, I have tried to run that and there is no NPE from that piece of code.
It is most probably that, you have reassigned your produit (I would strongly recommend you follow Java's common naming convention) to null, or assigned produit[n] to null in some other code.
I would suggest a quick check on which one is null:
Add before your assignment:
System.out.println("produit null ? " + (produit == null));
System.out.println("produit[n] null ? " + (produit[nbrProduit] == null));
it should tell you which one is null, and give you hints where you may have incorrect updated the reference.
Problem
According to your stack trace, NPE is caused by this line:
this.Produit[nbrProduct][0] = code;
Apparently, you found out via console output that what is null here is this.Produit.
Possible Cause
Since you said you initialized Produit with the following line:
private String[][] Produit = new String[99][3];
then your NPE should not happen, unless you have reset the reference to null somewhere via some code like:
Commande.Produit = null;
Finding write accesses
If you're using Eclipse, there is a way to find every write access to your attribute:
click on your attribute Produit in your Commande_Final class, so that it is highlighted
go to Search > Write Access > Project
check if there is any other write access than your initialization
My guess is that this.Produit[nbrProduct] is null.
It can be initialized: this.Produit[nbrProduct] = new String[someSize];
Assigning null never causes a NPE, the issue must be your array has been not initialized.
The array is initialized when the object is created, so my guess is that array is re-assigned before the setProduct method is called. Check if this.Produit[nbrProduct] is not null when setProduct is called.
PS: Please, use Java conventions: method, attribute and variable names start with lowcase, class/interface names start with uppercase. In general try to use CamelCase (except constants, that should be LIKE_THIS_EXAMPLE)

javax.xml.xpath.XPathExpressionException when trying to update values in XML

I have an application with an xml like this :
<root>
<info>
.....
</info>
<type>
<object_type>..</object_type>
<prop>
<data>
.... . ..
</prop>
<param>
....
</param>
<param>
....
</param>
<param>
....
</param>
etc
All this data is parsed from XML and now I the user should be able to edit the data.
So I have implemented a method for updating the params:
but its giving me an exception due to wrongly written Xpath statement :
javax.xml.xpath.XPathExpressionException
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source)
at xmleditor.service.XMLEditorService.updateParameters(XMLEditorService.java:96)
at xmleditor.gui.XmlEditorMain$5$1.tableChanged(XmlEditorMain.java:364)
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableCellUpdated(Unknown Source)
at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source)
at javax.swing.JTable.setValueAt(Unknown Source)
at javax.swing.JTable.editingStopped(Unknown Source)
at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source)
at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source)
at javax.swing.JTable$GenericEditor.stopCellEditing(Unknown Source)
at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(Unknown Source)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: javax.xml.transform.TransformerException: Ett platssteg förväntades efter token '/' eller '//'.
at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source)
at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
... 41 more
So what I have try to do is the get the rowCount from the table that the user edit and store it in an int x. Then I have stored the columnName in an String columnName. And I am trying to use this variables in my xpath expression to find the perfect location depending on the ObjectType the user has selected. and update and save it in the xml file. But somehow it seems that my xpath expression is wrong or that the transformer cannt read it somehow. since I also get this one :
Caused by: javax.xml.transform.TransformerException
this is my java code:
// Update the parameters
public void updateParameters(String columnName, Object data, int x,
Type type) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
File file = new File("xmlFiles/CoreDatamodel.xml");
System.out.println("Incoming String value : " + data);
System.out.println("Index value : " + x);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.compile(
"//OBJECT_TYPE[text() = '" + type.getObjectType()
+ "']/following-sibling::param/[position()='" + x
+ "']/'" + columnName + "'").evaluate(xmlDocument,
XPathConstants.NODESET);
// Set new NodeValue
node.setNodeValue(data.toString());
// Save the new updates
try {
save(file, xmlDocument);
} catch (Exception e) {
e.printStackTrace();
}
}
// Method for saving the new updated data.
public void save(File file, Document doc) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
// Holds the old document
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String s = writer.toString();
System.out.println(s);
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(s);
bufferedWriter.flush();
bufferedWriter.close();
}
how to fix this?
Update :
This code solves the problem but the node is null so the expression is working.
Node node = (Node) xPath.compile(
"//OBJECT_TYPE[text() = '" + type.getObjectType()
+ "']/following-sibling::param[position()= '" + x
+ "']/" + columnName + "").evaluate(xmlDocument,
XPathConstants.NODE);
So far, I see two problems:
param*
A name test in XPath doesn't work the same way as a glob. Broadly speaking, you either match a name completely, or you use * to match any node of the principal node type for that axis (normally an element).
As such, param* does not match things like param, param0, param-something-else, it's just an invalid expression.
/[
You can't have a predicate filtering nothing. A nodeset must be matched by a nodetest first, then filtered by a predicate.
There are two possibilities here:
The / is superfluous and must be removed (if you are trying to use the predicate to filter the nodeset returned by the expression preceding the /).
A nodetest must be inserted after the /, (if you are trying to filter the children or descendants of nodes within that nodeset).

Deleting xml elements not working because of null value

I have created a delete method.
My method has an index as argument that comes from a jlist position.
The method will get the position from the jlist that the user have clicked and look up that element in the xml and delete it and all children.
So if user click TrainCategorySpeed it has index 122 and will look for that type in the XML
<type>
<OBJECT_TYPE>TrainCategorySpeed</OBJECT_TYPE>
- <prop>
<DESCRIPTION>Differential speed limit</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>0</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>NULL</HELP>
<MIN_NO>NULL</MIN_NO>
<MAX_NO>NULL</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
- <param>
<PARAMETER>trainCategory</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>Train category</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>1</ORDER1>
<NESTED>1</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:15</FORMAT>
</param>
- <param>
<PARAMETER>speed</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>Speed (km/h (V_DIFF))</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>2</ORDER1>
<NESTED>1</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:600:5</FORMAT>
</param>
</type>
But when I am trying to achieve this it gives me a nullpointer exception
on the follwoing line : Node type = doc.getElementsByTagName("type").item(x);
x here is really giving correct position and type is the element in the xml.
but the type is null.
This is the rest of the method :
public void deleteObjType(int x) {
System.out.println("index : " + x); // This one will return index : 122 in the console and it does
File file = new File("xmlFiles/CoreDatamodel.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(file);
Node type = doc.getElementsByTagName("type").item(x);
System.out.println("type : " + type);
NodeList nodes = type.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node element = nodes.item(x);
System.out.println("element : " + element);
// remove node
if ("OBJECT_TYPE".equals(element.getNodeName())) {
type.removeChild(element);
}
}
// Save the new update
save(file, doc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Full stacktrace:
java.lang.NullPointerException
at cxmleditor.service.XMLEditorService.deleteObjType(XMLEditorService.java:106)
at xmleditor.gui.XmlEditorMain$deleteObjType.actionPerformed(XmlEditorMain.java:383)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I think your problem is the following line:
for (int i = 0; i < nodes.getLength(); i++) {
Note that i should not increment when you delete a node.
The problem is not necessarily that your type object is null. When deleteObjType(0); is called on the simple xml you provided,
System.out.println("type : " + type);
only uses the toString()-method of the type object instance and prints out:
type : [type: null]
if you instead do
System.out.println("type : " +type);
if(type == null) {
System.out.println("type is null!");
}else {
System.out.println("type is not null but an instance of ["+type.getClass().getCanonicalName()+"]");
}
it prints in my case:
type is not null but an instance of [com.sun.org.apache.xerces.internal.dom.DeferredElementImpl]
DOM is also a bit more complex than element nodes having direct element nodes as children as you can see here . I've learned through practice that simply printing out the nodes is usually not enough.
Which part of the xml do you actually want to remove?

Null pointer exception when access index of a DefaultListModel

I have a list:
JList characterList = new JList(characterListModel);
characterListModel = new DefaultListModel();
String myCharacters[]={"Dean Winchester","Sam Winchester",
"Bobby Singer","Castiel"};
for (String myCharacter : myCharacters) {
((DefaultListModel) characterList.getModel()).addElement(myCharacter);
}
And I've written a method, in a seperate class, to remove a character at a selected index:
public void removeCharacter() {
DefaultListModel characterListModel = ((PlayerContentPane) IViewManager.Util.getInstance()
.getMyContainerPane().getMyPlayerManagerPane().getContentPane())
.getCharacterListModel();
JList characterList = ((PlayerContentPane) IViewManager.Util.getInstance()
.getMyContainerPane().getMyPlayerManagerPane().getContentPane())
.getCharacterList();
int idx = characterList.getSelectedIndex(); //<---line 62
int size = characterListModel.getSize();
characterListModel.remove(idx);
if (size == 0) {
//do nothing
} else {
if (idx == characterListModel.getSize()) {
idx--;
}
characterList.setSelectedIndex(idx);
characterList.ensureIndexIsVisible(idx);
}
}
However when I run it with my button, I get this stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.detica.LarpDB.Controller.Controller.removeCharacter(Controller.java:62)
at com.detica.LarpDB.view.PlayerContentPane$3.actionPerformed(PlayerContentPane.java:94)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
A lot of the issues I've seen googling this, stem from the line: DefaultListModel characterListModel = ....
And the issue they have is that their creating a new instance of the object, now I''m only new to this whole Java thing, but this shouldn't be an issue with me, as I've not made anythign new, I've just been specific about which object this is.
Please could someone help me untangle myself?
Those 2 lines can create a lot of problems:
DefaultListModel characterListModel = ((PlayerContentPane) IViewManager.Util.getInstance()
.getMyContainerPane().getMyPlayerManagerPane().getContentPane())
.getCharacterListModel();
JList characterList = ((PlayerContentPane) IViewManager.Util.getInstance()
.getMyContainerPane().getMyPlayerManagerPane().getContentPane())
.getCharacterList();
If any of the chained methods return null (for whatever reason) the line will throw a NullPointerException.
I suggest you break them down in several lines and check the value of each call to see where you get the null from.
EDIT
Just realised that line 62 is int idx = characterList.getSelectedIndex();. It means that characterList is null.
If it's failing on the line that you indicated is line 62, then the only object referenced on that line, and therefore the only thing that could be null, is characterList. This implies that your getCharacterList function is returning null. (If the error was inside the getSelectedIndex function, then you would have another line in your stack trace.)
As you didn't post the getCharacterList function, I can't say much more. But you should take a look at that function and see under what circumstances it could return null.

Categories