How to Parse XML Response of Http Post in Java - java

I have an XML response from HTTP POST of this form
<PaymentResponse><TransDate>301111</TransDate><TransTime>011505</TransTime><SourceID>0</SourceID><Balance>0</Balance><ResponseCode>06</ResponseCode><ResponseMessage>Error XXXX</ResponseMessage><ApprovalCode>DECL</ApprovalCode><ApprovalAmount>0</ApprovalAmount></PaymentResponse>
But I am unable to parse it using Sax parser. Kindly help me out.
regards
I am using this code
public void endElement(String uri, String localName, String qName) throws SAXException {
if(localName.equalsIgnoreCase("TransDate"))
{
int tD = Integer.parseInt(currentValue);
tempResponse.setTDate(tD);
}
But every time localName comes with empty string.

As to your specific question: have a look at 'qName' argument instead: local name is only populated when parser uses namespace-aware mode. qName should contain "qualified" name, ie. concatenation of prefix (if any) and local name; so something like "ns:element" (if there is a prefix), or "element" (if no prefix).

Related

REST Assured doesn't accept curly brackets

Unable to use query in my Endpoint URL
I have tried using .queryParams() but it does not seem to work . I am getting the following error
java.lang.IllegalArgumentException: Invalid number of path parameters.
Expected 1, was 0.Undefined path parameters are:
cycle-id[12345];test.name[Validate_REST_Assured_Curly_Brackets].
Can someone help me out
almQuery=https://{almurl}/qcbin/rest/domains/{domain}/projects/{project}/test-instances?query={cycle-id[12345];test.name[Validate_REST_Assured_Curly_Brackets]}
Response response = RestAssured.given().relaxedHTTPSValidation()
.contentType("application/xml")
.cookie(cookie) .get(getEntityEndpoint(almQuery)).then().extract().response();
This is how RestAssured implementation works. Whenever your url contains curly braces it will expect path param with for that. For example, if your url contains {project} you should provide a path param with name project.
The only way to avoid it is by manually encoding { and } characters in your url. You could use URLEncoder.encode(), but it will mess your other characters so try simply replacing all { and } with encoded values:
public class App {
public static void main(String[] args) {
String url = "http://www.example.com/path/{project}";
String encoded = encodeUrlBraces(url);
RestAssured.given()
.when()
.get(encoded);
}
private static String encodeUrlBraces(String url) {
return url.replaceAll("\\{", "%7B").replaceAll("}", "%7D");
}
}
Here's an answer for this from Rest Assured founder and contributor https://github.com/rest-assured/rest-assured/issues/682

Parsing XML-response and store to a container

I'm making HTTP-queries to a website and response I get is in XML-format. What I want to do is make multiple queries, parse data and have them in an ArrayList or some other container so I can easily access each query's data. I've been using some time to play with SAX for parsing the response. Examples I read had XML format like this:
<?xml version="1.0"?>
<company>
<staff>
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
I managed to parse format like this pretty easily just by looking at the examples on the internet.
But in my case I need to parse data like this:
<?xml version="1.0" encoding="UTF-8"?>
<root response="True">
<movie title="A Good Marriage" year="2014" rated="R" released="03 Oct 2014" runtime="102 min" genre="Thriller" director="Peter Askin" writer="Stephen King (short story)" actors="Joan Allen, Anthony LaPaglia, Stephen Lang, Cara Buono" plot="After 25 years of a good marriage, what will Darcy do once she discovers her husband's sinister secret?" language="English" country="USA" awards="N/A" poster="http://ia.media-imdb.com/images/M/MV5BMTk3MjY2ODgwNl5BMl5BanBnXkFtZTgwMTQ0Mjg0MjE#._V1_SX300.jpg" metascore="43" imdbRating="5.1" imdbVotes="2,016" imdbID="tt2180994" type="movie"/>
</root>
And from this response I want parse all the things to some container, so it's easy to use. I'm still learning things, maybe someone can help me out here, point me to right direction? :) Making queries is not a problem but parsing and storing data is.
EDIT: So to be more clear, my problem is that response from server isn't in neat XML-format like in the first example, you can see it's like this:
<movie title="A Good Marriage" year="2014" rated="R" released="03 Oct 2014" runtime="102 min" genre="Thriller" director="Peter Askin" writer="Stephen King (short story)" actors="Joan Allen, Anthony LaPaglia, Stephen Lang, Cara Buono" plot="After 25 years of a good marriage, what will Darcy do once she discovers her husband's sinister secret?" language="English" country="USA" awards="N/A" poster="http://ia.media-imdb.com/images/M/MV5BMTk3MjY2ODgwNl5BMl5BanBnXkFtZTgwMTQ0Mjg0MjE#._V1_SX300.jpg" metascore="43" imdbRating="5.1" imdbVotes="2,016" imdbID="tt2180994" type="movie"/>
And when I run my code, it doesn't print out anything but when I modify XML a bit manually like this:
<?xml version="1.0" encoding="UTF-8"?>
<root response="True">
<movie> title="Oblivion" year="2013" rated="PG-13" released="19 Apr 2013" runtime="124 min" genre="Action, Adventure, Mystery" director="Joseph Kosinski" writer="Karl Gajdusek (screenplay), Michael Arndt (screenplay), Joseph Kosinski (graphic novel original story)" actors="Tom Cruise, Morgan Freeman, Olga Kurylenko, Andrea Riseborough" plot="A veteran assigned to extract Earth's remaining resources begins to question what he knows about his mission and himself." language="English" country="USA" awards="10 nominations." poster="http://ia.media-imdb.com/images/M/MV5BMTQwMDY0MTA4MF5BMl5BanBnXkFtZTcwNzI3MDgxOQ##._V1_SX300.jpg" metascore="54" imdbRating="7.0" imdbVotes="307,845" imdbID="tt1483013" type="movie"/>
</movie>
</root>
So I added ending tag > for the movie-element and ending tag </movie> to the end, my program prints it like:
Movie : title="Oblivion" year="2013" rated="PG-13" released="19 Apr 2013" runtime="124 min" genre="Action, Adventure, Mystery" director="Joseph Kosinski" writer="Karl Gajdusek (screenplay), Michael Arndt (screenplay), Joseph Kosinski (graphic novel original story)" actors="Tom Cruise, Morgan Freeman, Olga Kurylenko, Andrea Riseborough" plot="A veteran assigned to extract Earth's remaining resources begins to question what he knows about his mission and himself." language="English" country="USA" awards="10 nominations." poster="http://ia.media-imdb.com/images/M/MV5BMTQwMDY0MTA4MF5BMl5BanBnXkFtZTcwNzI3MDgxOQ##._V1_SX300.jpg" metascore="54" imdbRating="7.0" imdbVotes="307,845" imdbID="tt1483013" type="movie"/>
So basically code I'm using at the moment reads everything between <movie> and </movie>, problem is that original response from the server leaves movie tag open like this: <movie title="Oblivion"... and doesn't have </movie> tag either.
I've been struggling pretty long with this, hopefully someone understands my confusing explanation! At the moment my parser code looks like this:
public void getXml(){
try {
// obtain and configure a SAX based parser
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
// obtain object for SAX parser
SAXParser saxParser = saxParserFactory.newSAXParser();
// default handler for SAX handler class
// all three methods are written in handler's body
DefaultHandler defaultHandler = new DefaultHandler(){
String movieTag="close";
// this method is called every time the parser gets an open tag '<'
// identifies which tag is being open at time by assigning an open flag
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("MOVIE")) {
movieTag = "open";
}
}
// prints data stored in between '<' and '>' tags
public void characters(char ch[], int start, int length)
throws SAXException {
if(movieTag.equals("open")) {
System.out.println("Movie : " + new String(ch, start, length));
}
}
// calls by the parser whenever '>' end tag is found in xml
// makes tags flag to 'close'
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equalsIgnoreCase("MOVIE")) {
movieTag = "close";
}
}
};
// parse the XML specified in the given path and uses supplied
// handler to parse the document
// this calls startElement(), endElement() and character() methods
// accordingly
saxParser.parse("xml/testi.xml", defaultHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
Please anyone, help is greatly appreciated..
You can still use a SAX parser, which you've been learning. You didn't mention which parser you're using. I use xerxes (from Apache.org).
What you might want to do is implement a class that extends DefaultHandler. If you're using Eclipse as your IDE, you can have Eclipse implements stubs for all the methods from DefaultHandler, then add debug output to each of them to get a better feel for what happens.
But the important method is this:
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
All your fields (title, year, rated, etc) will be available in the attributes array.
Then what you'll get:
-A call to startElement for the
-A call to startElement for the
Plus other calls you don't care about. So once you understand what you're doing, you can delete the methods that are nothing but debug statements, if you want.

SAX Parsing in Android Searching Attribute

Im trying to identify an attribute using SAX. My XML is as follows
<DAY VALUE="1">
<BREAKFAST>0700</BREAKFAST>
<LUNCH>1200</LUNCH>
<DINNER>1900</DINNER>
</DAY>
<DAY VALUE="2">
<BREAKFAST>0730</BREAKFAST>
<LUNCH>1230</LUNCH>
<DINNER>1930</DINNER>
</DAY>
and my startElement code is as follows:
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementOn = true;
if (localName.equals("DAY"))
{
data = new XMLGettersSetters();
}
}
The above code will find the first element "DAY" and give me the contents but not the remaining DAY elements.
Is there a way I can search for the attribute? I am aiming to get the current day in the month and then search for the day within the xml. For example if today is the 2nd then I want to search in the XML file for DAY VALUE="2" and get the sub elements
==EDIT
If i modify my XML to
<DAY_1>
<BREAKFAST>0700</BREAKFAST>
<LUNCH>1200</LUNCH>
<DINNER>1900</DINNER>
</DAY_1>
<DAY_2>
<BREAKFAST>0730</BREAKFAST>
<LUNCH>1230</LUNCH>
<DINNER>1930</DINNER>
</DAY_2>
and code to:
if (localName.equals("DAY_2"))
{
data = new XMLGettersSetters();
}
I get a NullPointerException however the name if the class is not highlighted in logcat. It was referring to another class but i commented out those lines
you can get attribute value like this:
if (localName.equals("DAY")){
String day = "";
if (attributes.getValue("VALUE") != null) {
day = attributes.getValue("VALUE");
}
//data = new XMLGettersSetters();
}

Why did qName work and LocalName did not?

I was learning the Java SAX API. I made my own XML feed using php. Here is the XML Document
Now when i wanted to make my application output to the console nothing came up. I pinpointed the problem to the endElement method in my XMLHandler that extends DefaultHandler. Here is my implementation of it.
public void endElement(String uri, String localName, String qName) throws SAXException {
//I added the next three lines for debugging
System.out.println("Found End Element " + count + " times");
System.out.println("Localname = " + localName);
System.out.println("QName = " + qName);
super.endElement(uri, localName, qName);
if (this.currentItem != null){
if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMNAME)){
currentItem.setItemName(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMSITE)){
currentItem.setItemSite(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMNO)){
currentItem.setItemNo(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEM)){
System.out.println(currentItem);
items.add(currentItem);
}
builder.setLength(0);
}
count++;
}
Turns out that localName kept on coming empty hence the conditions never held true and the code never went into the decision block. On the other hand qName brought all names out properly and once i changed the variable to qName the List<Item> items collection type did fill up and worked correctly.
I am here to ask why did qName work and not localName? Whereas the tutorial from IBM's DeveloperWorks used an RSS feed and localName worked perfectly for him.
P.S. this is the feed the IBM Tutorial used: http://www.androidster.com/android_news.rss
As per the SAX namespace for Java API,
By default, an XML reader will report a Namespace URI and a localName
for every element that belongs in a namespace, in both the start and
end handler.
Perhaps, if you add a namespace to the XML and define your elements in that namespace, it would return a valid localName. The article also mentions that with namespace processing, some implementations will return empty qName.

Parsing http returned xml with java

So I've tried searching and searching on how to do this but I keep seeing a lot of complicated answers for what I need. I basically am using the Flurry Analytics API to return some xml code from an HTTP request and this is what it returns.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<eventMetrics type="Event" startDate="2011-2-28" eventName="Tip Calculated" endDate="2011-3-1" version="1.0" generatedDate="3/1/11 11:32 AM">
<day uniqueUsers="1" totalSessions="24" totalCount="3" date="2011-02-28"/>
<day uniqueUsers="0" totalSessions="0" totalCount="0" date="2011-03-01"/>
<parameters/>
</eventMetrics>
All I want to get is that totalCount number which is 3 with Java to an int or string. I've looked at the different DOM and SAX methods and they seem to grab information outside of the tags. Is there someway I can just grab totalCount within the tag?
Thanks,
Update
I found this url -http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser/
That helped me considering it was in android. But I thank everyone who responded for helping me out. I checked out every answer and it helped out a little bit for getting to understand what's going on. However, now I can't seem to grab the xml from my url because it requires an HTTP post first to then get the xml. When it goes to grab xml from my url it just says file not found.
Update 2
I got it all sorted out reading it in now and getting the xml from Flurry Analytics (for reference if anyone stumbles upon this question)
HTTP request for XML file
totalCount is what we call an attribute. If you're using the org.w3c.dom API, you call getAttribute("totalCount") on the appropriate element.
If you are using an SAX handler, override the startElement callback method to access attributes:
public void startElement (String uri, String name, String qName, Attributes atts)
{
if("day".equals (qName)) {
String total = attrs.getValue("totalCount");
}
}
A JDOM example. Note the use of SAXBuilder to load the document.
URL httpSource = new URL("some url string");
Document document = SAXBuilder.build(httpSource);
List<?> elements = document.getDescendants(new KeyFilter());
for (Element e : elements) {
//do something more useful with it than this
String total = (Element) e.getAttributeValue("totalCount");
}
class KeyFilter implements Filter {
public boolean matches (Object obj) {
return (Element) obj.getName().equals("key");
}
}
I think that the simplest way is to use XPath, below is an example based on vtd-xml.
import com.ximpleware.*;
public class test {
public static void main(String[] args) throws Exception {
String xpathExpr = "/eventMetrics/day/#totalCount";
VTDGen vg = new VTDGen();
int i = -1;
if (vg.parseHttpUrl("http://localhost/test.xml", true)) {
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot();
ap.selectXPath(xpathExpr);
ap.bind(vn);
System.out.println("total count "+(int)ap.evalXPathtoDouble());
}
}
}

Categories