0
I am having problems with XML return from a webservice when trying to parse. Netbeans output complains this way:
[Fatal Error] :1:13: White space is required between the destination of the processing instruction and the data. org.xml.sax.Saxparseexception; lineNumber: 1; columnNumber: 13; Whitespace is required between the processing instruction destination and the data.
The webservice is this: https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=11748933&retmode=xml
I did some research here to try to find some solution, but found only one topic on which the code is almost identical to mine.
Error happens in this conversion:
Document doc = dBuilder.parse(source);
Complete method of request:
private void aplicaMetodoXML() throws MalformedURLException, IOException, ParserConfigurationException, SAXException {
// conexão com o webservice
StringBuilder xmlContent = new StringBuilder();
URL url = new URL("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/epost.fcgi?db=pubmed&id=11237011");
HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
conexao.setRequestMethod("GET");
conexao.setRequestProperty("Content-Type", "text/xml");
conexao.setDoInput(true);
// tempo para requisição
conexao.setConnectTimeout(5000);
conexao.connect();
/* Pega o dado requisitado e joga na string */
Scanner scan = new Scanner(url.openStream());
while (scan.hasNext()) {
xmlContent.append(scan.next());
}
//System.out.println(xmlContent);]
// Trata conteúdo xml
String res = xmlContent.toString();
StringReader sr = new StringReader(res);
InputSource source = new InputSource(sr);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
// Aqui onde a exceção é chamada
Document doc = dBuilder.parse(source);
doc.getDocumentElement().normalize();
String teste = doc.getElementsByTagName("AbstractText").item(0).getTextContent();
System.out.println(teste);
}
I have tried but can’t find where the problem is. Is there any other alternative to parser?
That’s it, my dear. Thank you very much. Simple mistake that ends up getting in the way. Mostly because I don’t understand XML, so it would take me a long time to fix it - if I could do it myself. I had already found other alternatives in Python, but I needed this one. Thanks, brother!
– Mateus Vitor
I’m glad you solved it! More important than the mistake itself, is to understand why it’s happening. Good luck there!
– finx