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());
2
  • 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 or this Commented Apr 15, 2015 at 6:54
  • 1
    Your code is correct. Just parse your xml with the help of doc object. toString does not print the xml. Commented Apr 15, 2015 at 6:56

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

2 Comments

This is awesome! how can you select what type of xml attributes to display? insteead of displaying all of it.
how to get that xml string as return type?
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", "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.