Java NetBeans Send XML file to Web Service as String - Premature end of file error



I keep getting the following error when trying to send an XML file as a String to a Web Service in order to parse through it and find a term in that file. What is the proper way to send an XML file to a Web Service? The XML file that I am reading into a String is stored in my package and not in Web-Inf. Is that ok?


org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)



String path = "src/java/package/folders/File.xml";

String fileString = "";
File f = new File(file);
try
{

fileString = new String(Files.readAllBytes(Paths.get(file)));
find(fileString, needle);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}


find() method



private static boolean find(String fileString, java.lang.String needle)
{
util.Utils.client.Reader_Service service = new sc.util.XMLUtils.client.Reader_Service();
util.Utils.client.Reader port = service.getReaderPort();
return port.find(filename, needle);
}


and here's the method called in the Web Service



@WebMethod(operationName = "find")
public boolean find(@WebParam(name = "fileString") String fileString, @WebParam(name = "needle") String needle)
{
boolean found = false;


try
{

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(fileString));
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(bytes));

Document doc = db.parse(inputSource);
NodeList nodes = doc.getElementsByTagName("tag");

for (int i = 0; i < nodes.getLength(); i++)
{
Element element = (Element) nodes.item(i);

if(element.getAttribute("name").equalsIgnoreCase(needle))
{
found = true;
break;
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}

return found;
}

No comments:

Post a Comment