function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TerminusbotTerminusbot 

Soap Response: XmlStreamReader Method Help Syntax

After a succesful SOAP request. I am now trying to extract the <SagittaCode> value from the response body below. I am trying to use the XmlStreamReader method getAttributeValue, but not haveing much luck. 

I am getting an error of " Illegal State: Current state is not among the states START_ELEMENT , ATTRIBUTEvalid for getAttributeValue()". 

Thanks for the help! 
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <clientInsertResponse xmlns="http://amsservices.com/">
      <clientInsertResult>
        <Success>boolean</Success>
        <SagittaId>long</SagittaId>
        <SagittaCode>string</SagittaCode>
        <Errors>
          <Errors>
            <Code>int</Code>
            <Message>string</Message>
          </Errors>
          <Errors>
            <Code>int</Code>
            <Message>string</Message>
          </Errors>
        </Errors>
      </clientInsertResult>
    </clientInsertResponse>
  </soap12:Body>
</soap12:Envelope>

 
Best Answer chosen by Terminusbot
Greg HGreg H
You would use the getAttributeValue method if you were trying to parse XML content similar to:
<someObject SagittaCode="string" />
Where SagittaCode would be an attribute within the XML tag. Since you want the content between the starting & ending SagittaCode tags you would do something like:
String theSagittaCodeValue; //this will the variable that will hold your string
HTTPResponse externalResponse = http.send(externalRequest); //the response from the request
String bodyXML = externalResponse.getBody(); //grab the body of the response
XmlStreamReader reader = new XmlStreamReader(bodyXML); //construct an XmlStreamReader object
while(reader.hasNext()) { //while there are more XML events
	if (reader.getEventType() == XmlTag.START_ELEMENT) { //if this is the opening tag
		if ('SagittaCode' == reader.getLocalName()) { //if the tag is sagittacode
			while(reader.hasNext()) { //while there are more XML events
				if (reader.getEventType() == XmlTag.END_ELEMENT) { //if this is the closing tag
					break; //exist the loop
				} else if (reader.getEventType() == XmlTag.CHARACTERS) { //if this is the content between the tags
					theSagittaCodeValue = reader.getText(); //grab the content
				}
				reader.next(); //advance to the next XML event
			}
		}
	}
	reader.next(); //advance to the next XML event
}


Hope this helps.
-greg

All Answers

Greg HGreg H
You would use the getAttributeValue method if you were trying to parse XML content similar to:
<someObject SagittaCode="string" />
Where SagittaCode would be an attribute within the XML tag. Since you want the content between the starting & ending SagittaCode tags you would do something like:
String theSagittaCodeValue; //this will the variable that will hold your string
HTTPResponse externalResponse = http.send(externalRequest); //the response from the request
String bodyXML = externalResponse.getBody(); //grab the body of the response
XmlStreamReader reader = new XmlStreamReader(bodyXML); //construct an XmlStreamReader object
while(reader.hasNext()) { //while there are more XML events
	if (reader.getEventType() == XmlTag.START_ELEMENT) { //if this is the opening tag
		if ('SagittaCode' == reader.getLocalName()) { //if the tag is sagittacode
			while(reader.hasNext()) { //while there are more XML events
				if (reader.getEventType() == XmlTag.END_ELEMENT) { //if this is the closing tag
					break; //exist the loop
				} else if (reader.getEventType() == XmlTag.CHARACTERS) { //if this is the content between the tags
					theSagittaCodeValue = reader.getText(); //grab the content
				}
				reader.next(); //advance to the next XML event
			}
		}
	}
	reader.next(); //advance to the next XML event
}


Hope this helps.
-greg
This was selected as the best answer
TerminusbotTerminusbot
This worked. It appears when I look in the logs it is looping through the entire repsonse body before breaking even if it finds the Sagitta code. I will take a look and see if I can get it to break once the value is caputured. Thanks Greg!!