• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML Parsing issue.

 
Dave Ersk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
michael warren
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic