XMLSlurper inside Groovy 脚本不适用于简单的 XML
XMLSlurper inside Groovy Script Not Working for a simple XML
我正在尝试使用 Groovy 脚本的 XMLSlurper 插件解析 XML。
我需要读取 d:editStatus 元素中的值。
import groovy.xml.*;
def myxml = '<?xml version="1.0" encoding="utf-8"?>' +
'<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">' +
'<entry>' +
'<content type="application/xml">' +
'<m:properties>' +
'<d:key>JobApplication/applicationId=94319</d:key>' +
'<d:status>OK</d:status>' +
'<d:editStatus>UPDATED</d:editStatus>' +
'<d:message>Application has been updated successfully</d:message>' +
'<d:index m:type="Edm.Int32">0</d:index>' +
'<d:httpCode m:type="Edm.Int32">204</d:httpCode>' +
'<d:inlineResults m:type="Bag(SFOData.UpsertResult)"></d:inlineResults>' +
'</m:properties>' +
'</content>' +
'</entry>' +
'</feed>'
def mystatus = new XmlSlurper().parseText(myxml)
println mystatus
在这里,输出应该显示 xml 的对象形式,但它给了我以下输出
JobApplication/applicationId=94319OKUPDATEDApplication has been updated successfully0204
这很奇怪,因为我看不到任何元素,它连接所有值并显示为输出。我无法获取单个元素。
打印出您的 GPathResult 的值将默认打印出其节点的所有值。
要提取各个节点的值:
import groovy.xml.*
def myxml = '''
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<entry>
<content type="application/xml">
<m:properties>
<d:key>JobApplication/applicationId=94319</d:key>
<d:status>OK</d:status>
<d:editStatus>UPDATED</d:editStatus>
<d:message>Application has been updated successfully</d:message>
<d:index m:type="Edm.Int32">0</d:index>
<d:httpCode m:type="Edm.Int32">204</d:httpCode>
<d:inlineResults m:type="Bag(SFOData.UpsertResult)"></d:inlineResults>
</m:properties>
</content>
</entry>
</feed>
'''
def xml = new XmlSlurper().parseText(myxml)
println "key: ${xml.entry.content.properties.key}"
println "status: ${xml.entry.content.properties.status}"
println "editStatus: ${xml.entry.content.properties.editStatus}"
println "message: ${xml.entry.content.properties.message}"
println "index: ${xml.entry.content.properties.index}"
println "httpCode: ${xml.entry.content.properties.httpCode}"
println "inlineResults: ${xml.entry.content.properties.inlineResults}"
您还可以使用@
提取节点属性的值
println xml.entry.content.@type
我正在尝试使用 Groovy 脚本的 XMLSlurper 插件解析 XML。 我需要读取 d:editStatus 元素中的值。
import groovy.xml.*;
def myxml = '<?xml version="1.0" encoding="utf-8"?>' +
'<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">' +
'<entry>' +
'<content type="application/xml">' +
'<m:properties>' +
'<d:key>JobApplication/applicationId=94319</d:key>' +
'<d:status>OK</d:status>' +
'<d:editStatus>UPDATED</d:editStatus>' +
'<d:message>Application has been updated successfully</d:message>' +
'<d:index m:type="Edm.Int32">0</d:index>' +
'<d:httpCode m:type="Edm.Int32">204</d:httpCode>' +
'<d:inlineResults m:type="Bag(SFOData.UpsertResult)"></d:inlineResults>' +
'</m:properties>' +
'</content>' +
'</entry>' +
'</feed>'
def mystatus = new XmlSlurper().parseText(myxml)
println mystatus
在这里,输出应该显示 xml 的对象形式,但它给了我以下输出
JobApplication/applicationId=94319OKUPDATEDApplication has been updated successfully0204
这很奇怪,因为我看不到任何元素,它连接所有值并显示为输出。我无法获取单个元素。
打印出您的 GPathResult 的值将默认打印出其节点的所有值。
要提取各个节点的值:
import groovy.xml.*
def myxml = '''
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<entry>
<content type="application/xml">
<m:properties>
<d:key>JobApplication/applicationId=94319</d:key>
<d:status>OK</d:status>
<d:editStatus>UPDATED</d:editStatus>
<d:message>Application has been updated successfully</d:message>
<d:index m:type="Edm.Int32">0</d:index>
<d:httpCode m:type="Edm.Int32">204</d:httpCode>
<d:inlineResults m:type="Bag(SFOData.UpsertResult)"></d:inlineResults>
</m:properties>
</content>
</entry>
</feed>
'''
def xml = new XmlSlurper().parseText(myxml)
println "key: ${xml.entry.content.properties.key}"
println "status: ${xml.entry.content.properties.status}"
println "editStatus: ${xml.entry.content.properties.editStatus}"
println "message: ${xml.entry.content.properties.message}"
println "index: ${xml.entry.content.properties.index}"
println "httpCode: ${xml.entry.content.properties.httpCode}"
println "inlineResults: ${xml.entry.content.properties.inlineResults}"
您还可以使用@
提取节点属性的值println xml.entry.content.@type