1

I need to read the XML returned by API called in form of an URL, and convert in document format for further processing.

The URL is of form http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler. I referred the answer at read xml from url and used the following code. But the statement printed is "doc [#document: null]". What mistake am I doing?

    String pre_apiURL = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=";        
    String apiURL = pre_apiURL + celeb + "";
    apiURL = apiURL.replaceAll(" ","%20");
    System.out.println("url "+apiURL);
    URL url = new URL(apiURL);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(url.openStream());

    System.out.println("doc " + doc.toString());
Community
  • 1
  • 1
Bit Manipulator
  • 248
  • 1
  • 2
  • 17
  • 1
    `toString` is just printing information about the DOM, not that the XML content itself, you need to use a `Transformer` to write the XML back out again, something like [this](http://stackoverflow.com/questions/13925608/creating-xml-only-printing-to-one-line/13925622#13925622) or [this](http://stackoverflow.com/questions/27978151/how-to-remove-unwanted-tags-from-xml/27978451#27978451) – MadProgrammer Apr 15 '15 at 06:54
  • 1
    Your code is correct. Just parse your xml with the help of doc object. toString does not print the xml. – Vivek Gupta Apr 15 '15 at 06:56

3 Answers3

3

xyou can try like this, Here you can set your string response ang get xml string response into XML Document

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document doc;
        try {
            builder = factory.newDocumentBuilder();
            doc =  builder.parse(new InputSource( new StringReader("your xml string response")));
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            ex.printStackTrace();
        }

i'm not sure but i think it's helpful to you.

Akash Chavda
  • 1,185
  • 1
  • 11
  • 29
3

This is can help you quite a bit: Transforming XML

But if you do not wish to go read that, I have inserted a code snippet of the entire code you require to get and display the xml from the URL:

(Tried and Tested)


import javax.xml.parsers.DocumentBuilder;
import java.net.URL;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import java.io.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;

public class Test{

 public static void main(String[] args){
  
  try{ 
   String pre_apiURL = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&MaxHits=1&QueryString=Adam%20Sandler";        
   System.out.println("url "+ pre_apiURL);
   URL url = new URL(pre_apiURL);

   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   DocumentBuilder db = dbf.newDocumentBuilder();
   Document doc = db.parse(url.openStream());
   
   printDocument(doc, System.out);
   
  }catch(Exception e){}
 }
 
 public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer transformer = tf.newTransformer();
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
  transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

  transformer.transform(new DOMSource(doc), 
    new StreamResult(new OutputStreamWriter(out, "UTF-8")));
 }

}

Tested


All the best :) ..

Let me know of the outcome.

Good luck!

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
1

Here doc is your document

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
ACV
  • 9,964
  • 5
  • 76
  • 81