使用XpathSelectElements返回与for循环中的xml元素值匹配的节点

问题描述

这里使用XPathSelectElements,我将所有<Data_Set>选择到列表中。我只需要选择与我在for循环<Data_Set>中获得的匹配的foreach (var apiItem in wellBoreApi)<APINumber>我进入for循环并将其放入IEnumerable<XElement> apilistsval,但没有获得如何比较并获得该<Data_Set>唯一匹配的<APINumber>方法。 例如:如果{{1}中的<APINumber>是32145,则IEnumerable xnList应该只返回IEnumerable<XElement> apilistsval。如何获得此结果?

代码

<Data_Set>DI</Data_Set>

file.xml

XDocument doc1 = XDocument.Load(@"c:\file.xml");
List<XElement> wellBoreApi = doc1.Descendants("WellBore").Elements("APINumber").ToList();
foreach (var apiItem in wellBoreApi)
{
  IEnumerable<XElement> apilists = (from w1 in doc1.Descendants("WellBore")
  where wellBoreApi.Any(w2 => w1.Element("APINumber").Value == apiItem.Value)
  select w1).ToList();

 IEnumerable<XElement> apilistsval = apilists.Elements("APINumber");
 IEnumerable<XElement> xnList =   doc1.XPathSelectElements("//WellBore/MetaData/Data_Set");
} 
  

解决方法

我并不完全确定我了解您使用的所有限制。在我看来,当您遍历SEVERE: Exception while parsing file file:/Users/mikolaj/Documents/programowanie/NetBeansProjects/pstdio_services/pstdio_services-ear/target/gfdeploy/pstdio_services-ear/ java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@735ef671 rejected from java.util.concurrent.ThreadPoolExecutor@60f1aa16[Running,pool size = 20,active threads = 8,queued tasks = 30,completed tasks = 243] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) at org.glassfish.hk2.classmodel.reflect.Parser.parse(Parser.java:293) at com.sun.enterprise.v3.server.ReadableArchiveScannerAdapter.handleJar(ReadableArchiveScannerAdapter.java:197) at com.sun.enterprise.v3.server.ReadableArchiveScannerAdapter.onSelectedEntries(ReadableArchiveScannerAdapter.java:131) at org.glassfish.hk2.classmodel.reflect.Parser.doJob(Parser.java:345) at org.glassfish.hk2.classmodel.reflect.Parser.access$300(Parser.java:68) at org.glassfish.hk2.classmodel.reflect.Parser$3.call(Parser.java:304) at org.glassfish.hk2.classmodel.reflect.Parser$3.call(Parser.java:293) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) WARNING: result fault org.glassfish.hk2.classmodel.reflect.Parser$Result@1e051c19 Result for /Users/mikolaj/Documents/programowanie/NetBeansProjects/pstdio_services/pstdio_services-ear/target/gfdeploy/pstdio_services-ear/ SEVERE: Exception while deploying the app [pstdio_services-ear] SEVERE: Exception during lifecycle processing java.lang.IllegalArgumentException: Invalid ejb jar [pstdio_services-ejb-1.0.jar]: it contains zero ejb. Note: 1. A valid ejb jar requires at least one session,entity (1.x/2.x style),or message-driven bean. 2. EJB3+ entity beans (@Entity) are POJOs and please package them as library jar. 3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (@Stateless,@Stateful,@MessageDriven,@Singleton),please check server.log to see whether the annotations were processed properly. at org.glassfish.ejb.deployment.util.EjbBundleValidator.accept(EjbBundleValidator.java:149) at org.glassfish.ejb.deployment.util.EjbBundleValidator.accept(EjbBundleValidator.java:114) at com.sun.enterprise.deployment.BundleDescriptor.visit(BundleDescriptor.java:643) at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.visit(EjbBundleDescriptorImpl.java:767) at com.sun.enterprise.deployment.util.ApplicationValidator.accept(ApplicationValidator.java:121) at com.sun.enterprise.deployment.BundleDescriptor.visit(BundleDescriptor.java:643) at com.sun.enterprise.deployment.archivist.ApplicationArchivist.validate(ApplicationArchivist.java:723) at com.sun.enterprise.deployment.archivist.ApplicationArchivist.openWith(ApplicationArchivist.java:268) at com.sun.enterprise.deployment.archivist.ApplicationFactory.openWith(ApplicationFactory.java:232) at org.glassfish.javaee.core.deployment.DolProvider.processDOL(DolProvider.java:189) at org.glassfish.javaee.core.deployment.DolProvider.load(DolProvider.java:223) at org.glassfish.javaee.core.deployment.DolProvider.load(DolProvider.java:91) at com.sun.enterprise.v3.server.ApplicationLifecycle.loadDeployer(ApplicationLifecycle.java:934) at com.sun.enterprise.v3.server.ApplicationLifecycle.setupContainerInfos(ApplicationLifecycle.java:874) at com.sun.enterprise.v3.server.ApplicationLifecycle.prepare(ApplicationLifecycle.java:384) at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:540) at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:549) at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:545) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:360) at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:544) at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:575) at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:567) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:360) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:566) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1475) at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:111) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1857) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1733) at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:564) at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:251) at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:166) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:520) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:217) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53) at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:208) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53) at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:208) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549) at java.lang.Thread.run(Thread.java:748) 元素时,没有理由不抓住同级元素。这样,<WellBore><Data_Set>可以保证属于同一层次。 (请参阅代码段的第一部分)

如果出于某种原因您绝对必须手动遍历每个元素,则可以使用更高级的XPath(请参阅代码段的第二部分)。但这实际上不能保证您将获得相同的元素,除非您的<APINumber>是唯一的

APINumber