gpt4 book ai didi

java - Groovy:使用 JAX-B 对象的特定属性创建映射

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:00:20 26 4
gpt4 key购买 nike

我有一个示例 LEDES XML 文件 https://codebeautify.org/xmlviewer/cbdc79e7

使用 JDK 的 xjc 和 Ledes21.xsd 架构生成 Ledesxmlebilling21 类 https://codebeautify.org/xmlviewer/cb974a2e

xjc -d src ledes21.xsd

我正在使用 JAX-B 将 XML 转换为 Java 对象,如下所示

Ledesxmlebilling21 XMLtoObject(InputStream fis) throws Exception {
JAXBContext context = JAXBContext.newInstance(Ledesxmlebilling21.class)
Unmarshaller um = context.createUnmarshaller()
Ledesxmlebilling21 ledes = (Ledesxmlebilling21) um.unmarshal(fis)
return ledes
}

我正在尝试创建一个 map ,其中 Invoice 对象的 invId 属性值作为 Key,Values 作为所有 Invoice 对象的嵌套属性的 fileItemNbr 值的列表,如下所示

['Invoice 31' : [10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33] 
'Invoice 32' : [50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73]
]

有人可以帮我吗?

更新解决方案

def extractFileItemNbr(input, List<Integer> extracted) {
input.properties.each { prop, val -> //LedesXmlRuleProcessor.groovy:82)
if (prop in ["metaClass", "class"]) return
if (prop == 'file_item_nbr') {
extracted << val
} else {
extractFileItemNbr(val, extracted) //LedesXmlRuleProcessor.groovy:87)
}

}
}


def extractFileItemNbr(List input, List<Integer> extracted) {
input.each {
extractFileItemNbr(it, extracted)
}
}

void testExtract(Ledesxmlebilling21 ledesxmlebilling21) {
def xmlInvoices = ledesxmlebilling21.firm.client.invoice.flatten()
Map<String, List<Integer>> extracted = [:]
println "invoices -- "+xmlInvoices
for (Invoice invoice : xmlInvoices) {
def accuList = []
extractFileItemNbr(invoice, accuList)
extracted.put(invoice.invId, accuList)
}
println("extracted file_item_nbr "+ extracted)
}

我在实际 Ledesxmlebilling21 对象中遇到异常

Disconnected from the target VM, address: '127.0.0.1:59759', transport: 'socket'
2017-12-11 11:04:06 - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at org.codehaus.groovy.util.AbstractConcurrentMap.getOrPut(AbstractConcurrentMap.java:37)
at org.codehaus.groovy.reflection.GroovyClassValuePreJava7.get(GroovyClassValuePreJava7.java:94)
at org.codehaus.groovy.reflection.ClassInfo.getClassInfo(ClassInfo.java:143)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:265)
at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:879)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.createPojoMetaClassGetPropertySite(AbstractCallSite.java:351)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.createGetPropertySite(AbstractCallSite.java:327)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.acceptGetProperty(GetEffectivePojoPropertySite.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
at com.validation.rule.processor.impl.LedesXmlRuleProcessor.extractFileItemNbr(LedesXmlRuleProcessor.groovy:82)
at sun.reflect.GeneratedMethodAccessor82.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1027)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at com.validation.rule.processor.impl.LedesXmlRuleProcessor$_extractFileItemNbr_closure2.doCall(LedesXmlRuleProcessor.groovy:87)

最佳答案

我的猜测是该架构将具有循环属性。也许在这里看看:JAXB Mapping cyclic references to XML

关于java - Groovy:使用 JAX-B 对象的特定属性创建映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47717505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com