Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XMLStreamReader和对SOAP消息进行解组_Java_Xml_Soap_Xml Parsing - Fatal编程技术网

Java XMLStreamReader和对SOAP消息进行解组

Java XMLStreamReader和对SOAP消息进行解组,java,xml,soap,xml-parsing,Java,Xml,Soap,Xml Parsing,我在解码SOAP信封时遇到问题。 这是我的XML <?xml version="1.0"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:tns="http://c.com/partner/"> <env:Header>c <tns:MessageId env:mustUnderstand="true">3</tns:Message

我在解码SOAP信封时遇到问题。 这是我的XML

<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:tns="http://c.com/partner/">
  <env:Header>c
    <tns:MessageId env:mustUnderstand="true">3</tns:MessageId>
  </env:Header>
  <env:Body>
    <GetForkliftPositionResponse xmlns="http://www.c.com">
      <ForkliftId>PC006</ForkliftId>
     </GetForkliftPositionResponse>
  </env:Body>
</env:Envelope>
在xsr.nextTag()读取QName之后,您可以从中获得标记名和前缀

QName qname = xsr.getName();
String pref = qname.getPrefix();
String name = qname.getLocalPart();

最初,xsr指向文档事件之前(即XML声明),并且
nextTag()
前进到下一个标记,而不是下一个同级元素:

如果你想跳到身体上,一个更好的习惯用法是

boolean foundBody = false;
while(!foundBody && xsr.hasNext()) {
  if(xsr.next() == XMLStreamConstants.START_ELEMENT &&
     "http://www.w3.org/2003/05/soap-envelope".equals(xsr.getNamespaceURI()) &&
     "Body".equals(xsr.getLocalName())) {
    foundBody = true;
  }
}

// if foundBody == true, then xsr is now pointing to the opening Body tag.
// if foundBody == false, then we ran out of document before finding a Body

if(foundBody) {
  // advance to the next tag - this will either be the opening tag of the
  // element inside the body, if there is one, or the closing Body tag if
  // there isn't
  if(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
    // now pointing at the opening tag of GetForkliftPositionResponse
  } else {
    // now pointing at </env:Body> - body was empty
  }
}
boolean-foundBody=false;
而(!foundBody&&xsr.hasNext()){
if(xsr.next()==XMLStreamConstants.START_元素&&
"http://www.w3.org/2003/05/soap-envelope“.equals(xsr.getNamespaceURI())&&
“Body.equals(xsr.getLocalName())){
foundBody=true;
}
}
//如果foundBody==true,那么xsr现在指向开始的Body标记。
//如果foundBody==false,那么在找到一个Body之前,文档就用完了
if(foundBody){
//前进到下一个标记-这将是
//主体内的元素(如果有),或结束主体标记(如果有)
//没有
if(xsr.nextTag()==XMLStreamConstants.START_元素){
//现在指向GetForkliftPositionResponse的开始标记
}否则{
//现在指着——尸体是空的
}
}

我得到了异常[com.sun.istack.internal.SAXParseException2;行号:6;列号:3;意外元素(uri:,local:“Body”)。我想读取正文“后面”的内容,以便解组shellit@AhmedSaleh找到开头的
Body
标记后,就可以使用
xsr.nextTag()
一次,以前进到主体内元素的开始标记,并从那里开始解组。
    xsr.nextTag(); // Advance to opening envelope tag
    xsr.nextTag(); // advance to opening header tag
    xsr.nextTag(); // advance to opening MessageId
boolean foundBody = false;
while(!foundBody && xsr.hasNext()) {
  if(xsr.next() == XMLStreamConstants.START_ELEMENT &&
     "http://www.w3.org/2003/05/soap-envelope".equals(xsr.getNamespaceURI()) &&
     "Body".equals(xsr.getLocalName())) {
    foundBody = true;
  }
}

// if foundBody == true, then xsr is now pointing to the opening Body tag.
// if foundBody == false, then we ran out of document before finding a Body

if(foundBody) {
  // advance to the next tag - this will either be the opening tag of the
  // element inside the body, if there is one, or the closing Body tag if
  // there isn't
  if(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
    // now pointing at the opening tag of GetForkliftPositionResponse
  } else {
    // now pointing at </env:Body> - body was empty
  }
}