解析xsd文件后获取空结果

时间:2017-12-15 08:47:03

标签: java xpath xsd xml-parsing javax

public class XSDReader {
    public static void main(String[] args) {
        try {
            method1();
            method2();
        } catch (XPathExpressionException | ParserConfigurationException | IOException | SAXException e1) {
            e1.printStackTrace();
        }

    }

    private static void method1() throws XPathExpressionException, FileNotFoundException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expression= "//xs:simpleType[@name='ReferenceDateEnum']//xs:enumeration";
        NodeList result=(NodeList) xpath.evaluate(expression,new InputSource(new FileInputStream("cprd-schema-v4-1.xsd")), XPathConstants.NODESET);     
        print(result);
    }

    private static void method2() throws ParserConfigurationException, XPathExpressionException, SAXException, IOException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();
        final InputStream fileInputStream = new FileInputStream("cprd-schema-v4-1.xsd");
        Document doc = db.parse(fileInputStream);
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expression= "//xs:simpleType[@name='ReferenceDateEnum']//xs:enumeration";
        NodeList result=(NodeList)  xpath.compile(expression).evaluate(doc,XPathConstants.NODESET);
        print(result);


    }

    private static void print(NodeList result) {
        for(int i = 0; i < result.getLength(); i++) 
        {
            Element e = (Element) result.item(i);
            System.out.println(e.getAttribute("name") + " = " + e.getNodeValue());
        }
    }
}

输入文件如下:     

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:xmlns:centralbank.ie:anacredit" xmlns:cbi="urn:xmlns:centralbank.ie:anacredit" elementFormDefault="qualified" >
 <xs:simpleType name="ReferenceDateEnum">
    <xs:restriction base="xs:string">
      <xs:enumeration value="2017-09-30"/>
      <xs:enumeration value="2017-10-31"/>
      <xs:enumeration value="2017-11-30"/>
      <xs:enumeration value="2017-12-31"/>
      <xs:enumeration value="2018-01-31"/>
      <xs:enumeration value="2018-02-28"/>
      <xs:enumeration value="2018-03-31"/>
      <xs:enumeration value="2018-04-30"/>
      <xs:enumeration value="2018-05-31"/>
      <xs:enumeration value="2018-06-30"/>
      <xs:enumeration value="2018-07-31"/>
      <xs:enumeration value="2018-08-31"/>
      <xs:enumeration value="2018-09-30"/>
      <xs:enumeration value="2018-10-31"/>

    </xs:restriction>
  </xs:simpleType>

</xs:schema>

我使用this站点检查xpath是否正确并获取结果。我尝试了两种方法method1()和method2(),但结果是一样的。我的目标是获取值的集合,但我得到空的结果节点列表,请建议正确的方法来获取它。

1 个答案:

答案 0 :(得分:0)

我在method1()

中进行了以下更改
private static void method1() throws XPathExpressionException, FileNotFoundException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    HashMap<String, String> prefMap = new HashMap<String, String>();
    prefMap.put("xs", "http://www.w3.org/2001/XMLSchema");
    prefMap.put("cbi","urn:xmlns:centralbank.ie:anacredit");
    xpath.setNamespaceContext(new SimpleNamespaceContext(prefMap));
    String expression="//xs:simpleType[@name='ReferenceDateEnum']//xs:enumeration";
    NodeList result=(NodeList) xpath.evaluate(expression,new InputSource(new FileInputStream(new File("cprd-schema-v4-1.xsd"))), XPathConstants.NODESET);       
    print(result);
}

我从org.springframework.util.xml复制了SimpleNamespaceContext类,但它运行良好。