GeeCON Prague 2014*
The moose likes Web Services and the fly likes XML Parsing issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "XML Parsing issue." Watch "XML Parsing issue." New topic
Author

XML Parsing issue.

Dave Ersk
Greenhorn

Joined: Dec 28, 2002
Posts: 6
When I try and retrieve the attributes in the below XML via the below
methods NULL is alway's returned.

Tried using namespace/localpart and also to retrieve by tag name etc to no avail.

I'm able to see the values in the nodelist created:
NodeList nl = _resp.getFirstChild().getChildNodes();

Please let me know what methods I need to use to retrieve the values in the attributes??

Thanks!!
=========

Various methods tried, NULL returned for all:

_resp is an element.

Tried several methods such as paste below:
String str1 = "LK-FULL-DATE-OUT";
String str2 = "LK-FULL-TIME-OUT";
String str3 = "LK-APPLID-OUT";
String str4 = "LK-SYSID-OUT";
String str5 = "LK-EIBTASKN-NBR-OUT";

String attri1 = _resp.getAttribute(str1);
String attri2 = _resp.getAttribute(str2);
String attri3 = _resp.getAttribute(str3);
String attri4 = _resp.getAttribute(str4);
String attri5 = _resp.getAttribute(str5);

String namespace1= "http://runFast01.CommareaTest.x4mlsoa.com/CA/SOLAPC99/SO9#D001";
String localpart1 = "defaultResponse";
String attr02 = _resp.getAttributeNS(namespace1,localpart1);

=============
XML to retrive attributes values from =
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><defaultResponse xmlns="http://runFast01.CommareaTest.x4mlsoa.com/CA/SOLAPC99/SO9#D001"><DfhCommarea LK-NAME-OUT="" LK-FULL-DATE-OUT="03/15/2007" LK-FULL-TIME-OUT="10:06:45" LK-APPLID-OUT="A07TPCIA" LK-SYSID-OUT="TPCA" LK-EIBTASKN-NBR-OUT="0032113"></DfhCommarea></defaultResponse></soap:Body></soap:Envelope>
[ March 16, 2007: Message edited by: Jersey Cowboy ]

Best Regards,<br />Dave Ersk<br />SCJP 1.4
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5


Thats a bad idea since it depends on the order of the nodes and a single TEXT node will screw it up. You should be using the getElementsByTagName() method, specifying the "DfhCommarea" tag name - this will ensure that you are working with the right element when looking for attributes.

Bill
Dave Ersk
Greenhorn

Joined: Dec 28, 2002
Posts: 6
Thanks for the tip Bill!!

Here's the complete working code for getting attributes from XML response.

Values and tags are added to name/value pair for return..

NodeList Dfhcommarea1 = _resp.getElementsByTagName("DfhCommarea");
Node childNode = Dfhcommarea1.item(0);
NamedNodeMap nnmap = childNode.getAttributes();
int nAtts=nnmap.getLength();
for(int j=0;j<nAtts;++j) {
Node node=nnmap.item(j);

if (node.getNodeName().toString().equals("LK-FULL-DATE-OUT")) {
fulldateout = node.getNodeValue();
nv.add(id++,"LK-FULL-DATE-OUT",fulldateout);
};
if (node.getNodeName().toString().equals("LK-FULL-TIME-OUT")) {
fulltimeout = node.getNodeValue();
nv.add(id++,"LK-FULL-TIME-OUT",fulltimeout);
};
if (node.getNodeName().toString().equals("LK-APPLID-OUT")) {
fullapplidout = node.getNodeValue();
nv.add(id++,"LK-APPLID-OUT",fullapplidout);
};
if (node.getNodeName().toString().equals("LK-SYSID-OUT")) {
fullsysidout = node.getNodeValue();
nv.add(id++,"LK-SYSID-OUT",fullsysidout);
};
if (node.getNodeName().toString().equals("LK-EIBTASKN-NBR-OUT")) {
fulleibtasknout = node.getNodeValue();
nv.add(id++,"LK-EIBTASKN-NBR-OUT",fulleibtasknout);
};
};
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Welcome to the Ranch "DaveErsk",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

So in order to be compliant, all you need to do is to put the space between your first and last names.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
michael warren
Ranch Hand

Joined: Oct 20, 2006
Posts: 50
Wasn't sure where _resp variable comes from or what type it is so couldn't replicate what you're doing, but the following code worked for me

Dave Ersk
Greenhorn

Joined: Dec 28, 2002
Posts: 6
Thanks for the code snippet, very clean!!

The object comes into to the method as an element
public CommonOutput(Element _resp) throws SOAPException ..

From there I was having a hard time finding my node to extract the attributes..

Not any more:
 
GeeCON Prague 2014
 
subject: XML Parsing issue.