I am getting html response from server i want xml response from my webserver so Which plugin to use to get XML response from server. Can any one help me regarding this it would be very helpful.
Keep in mind that having an xml from a webserver, file or whatever is the same for the Java XPath API.
So, you have to apply this following :
//Partie connexion
URL url = new URL(addressToConnect);
HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
connexion.connect();
InputSource geocoderResultInputSource;
geocoderResultInputSource = new InputSource(connexion.getInputStream());
//Partie XML/XPATH
Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeListCodeResult = (NodeList) xpath.evaluate("//status", geocoderResultDocument, XPathConstants.NODESET);
etc...
More examples here, from a project that I'm making
Hope it's helps ;)
Related
I want to make a web parser in java. I'm using jsoup. But I got an error like this
how to fix it? is it because of my classpath?
also this are my imports
Use below code.
Document doc = Jsoup.connect(I).get();
Elements links = doc.select("div#content > p").first();
You have to get the Document from Connection.
Connection con = Jsoup.connect(url);
Document doc = con.get();
I am performing simple RESTFUL service API verification in Java.
To handle response in JSON format is very convenient. Using org.json library, it's easy to convert JSON string from RESTFUL response into JSON object, and compare it with that of the expected JSON string.
JSONObject response = new JSONObject(json_response_str);
JSONObject expected = new JSONObject(json_expected_str);
JSONAssert.assertEquals(expected, response, JSONCompareMode.LENIENT);
If it is some element of the JSON response that need to compare, it is also easy because it is easy to extract sub element from JSONObject using APIs like:
JSONObject element_in_response = response.get("..."); or
JSONObject element_in_response = response.getJSONObject("...");
However, to handle response in XML format, things are more difficult. To compare the whole XML response with expected XML is not bad, I can use XMLUnit to do it:
String xml_response_str = ...
String xml_expected_str = ...
assertXMLEquals(xml_response_str, xml_expected_str);
However, there's no such things like xmlOject as there is in JSON.
So what do I do if want to compare some element of the XML response with expected?
I've search forums and JAXB is sometimes mentioned. I checked and it is about parsing XML to Java object. So am I supposed to parse both response XML string and expected XML string, then extract the element as Java object, then compare them? It seems complicated, not to mention I need the XML schema to start with.
What is the effective way to do this, is there anything that is as convenient as in the case of JSON?
Thanks,
You can try to use XPATH.
There is a short example.
Here is XML string:
<?xml version="1.0" encoding="UTF-8"?>
<resp>
<status>good</status>
<msg>hi</msg>
</resp>
The folowing code will get status and message:
String xml = "<resp><status>good</status><msg>hi</msg></resp>";
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
InputSource source = new InputSource(new StringReader(xml));
Document doc = (Document) xpath.evaluate("/", source, XPathConstants.NODE);
String status = xpath.evaluate("/resp/status", doc);
String msg = xpath.evaluate("/resp/msg", doc);
System.out.println("status=" + status);
System.out.println("Message=" + msg);
Here is more examples about how to use XPATH:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
There are a number of ways for testing XML. Converting XML to JSON not being one of them, but can be done.
Testing XML is usually performed using XPath style comparisons which focus on elements, attributes and content and not so much on comparing chunks.
From looking at your code you're already familiar with XML assertions from http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/XMLAssert.htm but you might also want to look at http://www.w3schools.com/xsl/xpath_intro.asp.
XML validation is not that easy and does require a lot of effort to begin with. Once you've got your tesing tools in order it gets a whole lot easier.
verify (or extract) XML is independent from protocol, RESTful service, etc. , but it is normally used in SOAP services.
Comparison with JSON is interesting. JSON is more easy to use with php, javascript, ...
If you want to connect two java servers, XML is sufficient, or plain java Objects (not portable solution with other languages).
Better point to use XML: it is more, more powerfull, well standardized, and you have lot of tools to process it.
What you are asking: equivalent of JSONobject exists in XML for a while: it is a DOM document, or a Node.
1 read your XML
String xml="<root>content</root>";
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE
Document document = builder.parse(new InputSource(new StringReader(xml)));
2 Best way to get some particular data: XPath: you give some path to your datas (root/group/class1/other_group/...), you can put wildcards (*), select about parameters, values, etc.
see this:
How to read XML using XPath in Java
XPath xpath = XPathFactory.newInstance().newXPath();
String expression="/root";
3 you can get direct values
expression="/root/text()";
String value = xpath.evaluate(expression, document);
4 or you get all data (if several)
XPathExpression expr = xpath.compile(expression) ;
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
{
Node nodeSegment = nodes.item(i);
if (nodeSegment.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nodeSegment;
System.out.println("TAG="+eElement.getTagName());
System.out.println("VALUE="+eElement.getNodeValue());
I have a website that calls a __doPostBack for a specific link. I have tried loading the page that the link loads, but inputting POST data manually and also setting the __EVENTTARGET and __EVENTARGUMENT manually, but I keep receiving an error page.. If anyone has used the JSOUP library and has found a workaround for this problem please let me know. Here is the code for calling the website with POST data:
Connection.Response res = Jsoup.connect("https://parentaccess.ocps.net/Progress/ProgressSummary.aspx?T=2").data(target.substring(0,target.length()-5)+"txtClass_DBID",dBID).data("__LASTFOCUS","").data("__EVENTVALIDATION", eventValidation).data("__VIEWSTATE", viewState).cookie("ASP.NET_SessionId", cookie).data("__EVENTTARGET",target).data("DropDownListGradingPeriod","3").data("__EVENTARGUMENT","").header("Content-Type","text/html; charset=utf-8").header("Connection", "keep-alive").header("Cache-Control", "private").method(Method.POST).execute();
Document doc = res.parse();
Document doc2 = Jsoup.connect("https://parentaccess.ocps.net/Progress/ProgressDetails.aspx").data(target.substring(0,target.length()-5)+"txtClass_DBID",dBID).data("__LASTFOCUS","").data("__EVENTVALIDATION", eventValidation).data("__VIEWSTATE", viewState).cookie("ASP.NET_SessionId", cookie).data("__EVENTTARGET",target).data("DropDownListGradingPeriod","3").data("__EVENTARGUMENT","").header("Content-Type","text/html; charset=utf-8").header("Connection", "keep-alive").header("Cache-Control", "private").get();
Please note that I have tried both the Connection.Response and the JSOUP.connect with POST data, but have been receiving errors for doc2 (res will load the page, but the information must not be passed because no table is generated with the given POST data). Thanks!
Use HTMLUNIT for handling javascript.
After spending few days the only solution what i found was htmlunit.
Here is the working code:
HtmlPage page = webClient.getPage("PAGE_URL");
InputStream inputStream =
page.getElementById({EL_ID}").click().getWebResponse().getContentAsStream();
OutputStream outputStream = new FileOutputStream(new File("FILE_NAME"));
IOUtils.copy(inputStream, outputStream);
In the past ive used xpath to find the value of specific nodes that came from an xml ducemnt from a URL. Now i want to use this same code but from an xml document that is stored locally on the android phone at say sdcard/images/xml/newxml.xml
Here is the old code that i would like to be able to implement to use this, i just cannot figure out how to use the local xml file instead of a URL.
URL url = new URL("UrlWentHere");
InputSource xml = new InputSource(url.openStream());
XPath xpath = XPathFactory.newInstance().newXPath();
datafromxml = xpath.evaluate("//forecast_conditions[1]/high/#data", xml);
I don't quite understand the question. Why not just URL url = new URL("sdcard/images/xml/newxml.xml"); - or does the problem have to do with the app's restricted access to the file system?
I'm building an android app that communicates to a web server and am struggling with the following scenario:
Given ONE line of XML in a String eg:
"<test one="1" two="2" />"
I would like to extract the values into a HashMap so that:
map.get("one") = "1"
map.get("two") = "2"
I already can do this with a full XML document using the SAX Parser, this complains when i try to just give it the above string with a MalformedUrlException: Protocol not found
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc = null;
builder = factory.newDocumentBuilder();
doc = builder.parse("<test one="1" two="2" />"); //here
I realize some regex could do this but Id really rather do it properly.
The same behaviour can be found at http://metacpan.org/pod/XML::Simple#XMLin which is what the web server uses.
Can anyone help? Thanks :D
DocumentBuilder.parse(String) treats the string as a URL. Try this instead:
Document doc = builder.parse(new InputSource(new StringReader(text)));
(where text contains the XML, of course).