Java XmlSlurper不会解析整个XML
在WS-Lite插件中使用XmlSlurper处理XML字符串后,我试图访问该字符串的部分内容,以防万一。下面是一个示例XML字符串:Java XmlSlurper不会解析整个XML,java,xml,parsing,groovy,xmlslurper,Java,Xml,Parsing,Groovy,Xmlslurper,在WS-Lite插件中使用XmlSlurper处理XML字符串后,我试图访问该字符串的部分内容,以防万一。下面是一个示例XML字符串: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetBusinessObjectByPublicIdResponse>
<GetBusinessObjectByPublicIdResult>
<BusinessObject REF="21cf6434ae" Name="Incident" ID="1518">
<FieldList>
<Field REF="f5b2ef7e04" Name="ID">
93e5346ec110eee46ea095
</Field>
[tons more field entries]
</FieldList>
</BusinessObject>
</GetBusinessObjectByPublicIdResult>
</GetBusinessObjectByPublicIdResponse>
</soap:Body>
</soap:Envelope>
返回正确的NodeChild对象。但是,如果我执行以下操作:
def node = body.GetBusinessObjectByPublicIdResponse[0].GetBusinessObjectByPublicIdResult[0].BusinessObject[0]
body.GetBusinessObjectByPublicIdResponse[0].GetBusinessObjectByPublicIdResult[0].text()
我得到一个没有孩子的东西。看起来BusinessObject节点及其所有子节点都没有被解析。如果我执行以下操作,它们将作为字符串存在:
def node = body.GetBusinessObjectByPublicIdResponse[0].GetBusinessObjectByPublicIdResult[0].BusinessObject[0]
body.GetBusinessObjectByPublicIdResponse[0].GetBusinessObjectByPublicIdResult[0].text()
但它们不作为解析对象存在
这是我第一次不得不在Groovy中处理XMLs,所以我可能做错了什么,或者可能只是有些问题。非常感谢您的帮助。这对我很有用
def xml = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
| <GetBusinessObjectByPublicIdResponse>
| <GetBusinessObjectByPublicIdResult>
| <BusinessObject REF="21cf6434ae" Name="Incident" ID="1518">
| <FieldList>
| <Field REF="f5b2ef7e04" Name="ID">93e5346ec110eee46ea095</Field>
| </FieldList>
| </BusinessObject>
| </GetBusinessObjectByPublicIdResult>
| </GetBusinessObjectByPublicIdResponse>
</soap:Body>
</soap:Envelope>""".stripMargin().stripIndent()
def parse = new XmlSlurper().parseText( xml )
assert '93e5346ec110eee46ea095' == parse.Body
.GetBusinessObjectByPublicIdResponse
.GetBusinessObjectByPublicIdResult
.BusinessObject
.FieldList
.Field
.text()
在没有节点列表之前,您不必与节点列表打交道。在这里,我看到唯一可能的节点列表是FieldList。结果表明,问题在于XML字符串(它是SOAP响应)的格式不正确。所有未被解析的节点实际上都在使用
< and >
而不是<和>。然而,XmlSlurper实际上是将符号转换为<和>,而不是解析它们。因此,在XmlSlurper完成它的工作之后查看XML使一切看起来都是正确的。这是无效的XML。缺少。抱歉,这是复制/粘贴错误。。。我编辑了这个问题来解决这个问题。