Bit Manipulator
Bit Manipulator

Reputation: 358

Reading XML file from URL in java

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());

Upvotes: 1

Views: 10840

Answers (3)

Akash Chavda
Akash Chavda

Reputation: 1227

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.

Upvotes: 3

TejjD
TejjD

Reputation: 2561

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!

Upvotes: 3

ACV
ACV

Reputation: 10552

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", "");

Upvotes: 1

Related Questions