Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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 使用xpath e4.EMF.xpath.XPathContext查询EMF模型_Java_Xml_Xpath_E4_Emf - Fatal编程技术网

Java 使用xpath e4.EMF.xpath.XPathContext查询EMF模型

Java 使用xpath e4.EMF.xpath.XPathContext查询EMF模型,java,xml,xpath,e4,emf,Java,Xml,Xpath,E4,Emf,我有一个emf模型,我根据一些条件从中选择了一些对象 为此,我尝试使用org.eclipse.e4.emf.xpath.XPathContext。但是XPATH表达式没有给出任何结果 下面是示例xml <?xml version="1.0" encoding="ASCII"?> <codeAnalysis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation

我有一个emf模型,我根据一些条件从中选择了一些对象

为此,我尝试使用org.eclipse.e4.emf.xpath.XPathContext。但是XPATH表达式没有给出任何结果

下面是示例xml

<?xml version="1.0" encoding="ASCII"?>
   <codeAnalysis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="http://myTool/codeAnalysis/CommonReport">
<Header>
<Title>My_Report</Title>
<Date>02/05/2016</Date>
<CreatedBy>xyz</CreatedBy>
<Software>
  <checkpoint>0</checkpoint>
  <StartComponent>ABC</StartComponent>
  <StartFolder>E:\TestProject</StartFolder>
</Software>
<Environment>
  <Version>8.1.2</Version>
  <ToolVersion>1.33.0.qualifier</ToolVersion>      
</Environment>
</Header>
<Report>
<Component name="MSI">
    <File name="TestProject/comp/a.h">
        <Function name="TIME_STAMP_GET_TIMER_VALUE">
            <Complain lfd="1">
                <MetricName>STM29</MetricName>
                <Value>0</Value>
             </Complain>
        </Function>
    </File>
</Component>
</Report>
</codeAnalysis>

我的报告
02/05/2016
xyz
0
基础知识
E:\TestProject
8.1.2
1.33.0.1限定符
STM29
0
我想获得codeAnalysis/Header/Software/StartComponent的值

下面是我编写的代码

    public void emfQueryTest() {
    try {
        String filePath = "E:\\03Projects\\Test.xml";
        ResourceSet resourceSet = new ResourceSetImpl();
        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new MetricCommonReportResourceFactoryImpl());
        resourceSet.getPackageRegistry().put(MetricCommonReportPackage.eNS_URI, MetricCommonReportPackage.eINSTANCE);
        Resource resource = (Resource)resourceSet.getResource(URI.createFileURI(filePath), true);
        if (resource != null) {
            resource.load(Collections.EMPTY_MAP);
            if (resource.getContents() != null && resource.getContents().size() > 0) {
                CodeAnalysis metricCodeAnalysis = (MetricCommonReport.CodeAnalysis) resource.getContents().get(0);
                System.out.println(metricCodeAnalysis.getHeader().getTitle());
                XPathContextFactory<EObject> f = EcoreXPathContextFactory.newInstance();
                XPathContext xPathContext = f.newContext(metricCodeAnalysis);
                Iterator<MetricCommonReport.Software> it = xPathContext.iterate("/codeAnalysis/Header/Software");                   

                while( it.hasNext() ) {
                    System.out.println("Hello");
                    System.out.println("    " + it.next().getStartComponent());
                }  
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void emfQueryTest(){
试一试{
String filePath=“E:\\03Projects\\Test.xml”;
ResourceSet ResourceSet=new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().GetExtensionFactoryMap().put(“xml”,新MetricCommonReportResourceFactoryImpl());
resourceSet.getPackageRegistry().put(MetricCommonReportPackage.eNS_URI,MetricCommonReportPackage.eINSTANCE);
Resource Resource=(Resource)resourceSet.getResource(URI.createFileURI(filePath),true);
if(资源!=null){
resource.load(Collections.EMPTY_MAP);
if(resource.getContents()!=null&&resource.getContents().size()>0){
CodeAnalysis metricCodeAnalysis=(MetricCommonReport.CodeAnalysis)resource.getContents().get(0);
System.out.println(metricCodeAnalysis.getHeader().getTitle());
XPathContextFactory f=EcoreXPathContextFactory.newInstance();
XPathContext XPathContext=f.newContext(metricCodeAnalysis);
迭代器it=xPathContext.iterate(“/codeAnalysis/Header/Software”);
while(it.hasNext()){
System.out.println(“你好”);
System.out.println(“+it.next().getStartComponent());
}  
}
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
此xml已正确加载。但它并没有为startComponent打印任何值。实际上迭代器没有任何元素?我哪里出错了?请有人纠正我


Java中相同的XPath表达式给出了正确的节点和值。

您的代码似乎使用了
/qacdeanalysis
而不是
/codelanalysis
作为根元素。即使我使用/codelanalysis,也会得到相同的结果。