代码之家  ›  专栏  ›  技术社区  ›  VijayD

解析xsd文件后获取空结果

  •  0
  • VijayD  · 技术社区  · 8 年前
    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 回复  |  直到 8 年前
        1
  •  0
  •   VijayD    8 年前

    我在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复制了SimpleNamespaceContext类。springframework。util。xml和它的工作。