Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

using DOM to parse SOAP fault doesn't work properly

843834Oct 8 2002 — edited Oct 9 2002
why, when I run the following:

import java.io.IOException;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ParseFaultDom {
public void operation(String uri) {
System.out.println("Parsing XML File: " + uri + "\n\n");

String faultCode = "";
String faultString = "";

try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(uri);
// should only be one apiece here
NodeList faultCodes = document.getElementsByTagName("faultcode");
NodeList faultStrings=document.getElementsByTagName("faultstring");
Node codeNode = faultCodes.item(0);
Node stringsNode = faultStrings.item(0);

faultCode = codeNode.getNodeValue();
faultString = stringsNode.getNodeValue();
System.out.println("code: " + faultCode);
System.out.println("string: " + faultString);
} catch(ParserConfigurationException e) {
System.out.println("Error creating parser: " + e.getMessage( ));
} catch(IOException e) {
System.out.println("Error reading URI: " + e.getMessage( ));
} catch (SAXException e) {
System.out.println("Error in parsing: " + e.getMessage( ));
}
}

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java ParseFault [XML URI]");
System.exit(0);
}
String uri = args[0];
ParseFaultDom pfd = new ParseFaultDom();
pfd.operation(uri);
}
}

giving it the following xml file as input:

<?xml version='1.0' encoding='UTF-8'?>
<s:Envelope xmlns:s="http://www.w3.org/2001/06/soap-envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<s:Body>
<s:Fault>
<faultcode xsi:type="xsd:string">Client</faultcode>
<faultstring xsi:type="xsd:string">
Invalid value given for identifier field: "-1".
</faultstring>
<details>
<a>a</a>
<b>b</b>
<c>c</c>
</details>
</s:Fault>
</s:Body>
</s:Envelope>


do I get the following output:

Parsing XML File: fault.xml

code: null
string: null


In other words, it finds the tags I'm looking for, but it isn't getting the tag value, apparently.... I can add more instances of each tag, or delete them altogether, and the program raises the appropriate errors.... so it's is finding the tags inside the file, it just apparently doesn't pick up the value for some reason....

thanks anyone who can help... this should be simple, but it doesn't want to work for me... bad karma?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2002
Added on Oct 8 2002
2 comments
210 views