解析XSD文件以从复杂类型中获取元素名称

时间:2016-05-19 13:26:24

标签: java xml parsing xsd

我试图在XSD文件中获取元素名称。让我们说我有这个xsd:

<xsd:complexType name="SomethingOne">
        <xsd:sequence>
            <xsd:element name="Id" type="xsd:int"/>
            <xsd:element name="Time" type="xsd:string"/>
            <xsd:element name="Location" type="xsd:string"/>
            <xsd:element name="Building" type="xsd:string"/>
            <xsd:element name="Comments" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="SomethingTwo">
        <xsd:sequence>
            <xsd:element name="Id" type="xsd:int"/>
            <xsd:element name="Time" type="xsd:string"/>
            <xsd:element name="Location" type="xsd:string"/>
            <xsd:element name="Building" type="xsd:string"/>
            <xsd:element name="Phone" type="xsd:string"/>
            <xsd:element name="Device" type="xsd:string"/>
            <xsd:element name="Protocol" type="xsd:string"/>
            <xsd:element name="Comments" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>

对于SomethingOne,我想打印出所有元素名称(例如IdTimeLocation等...)。这是我的Java代码:

public void parse(String id) {
    try {
        // Setup classes to parse XSD file for complex types
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream("filepath.xsd"));

        // Given the id, go to correct place in XSD to get all the parameters
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList result = (NodeList) xpath.compile(getExpression(id)).evaluate(doc, XPathConstants.NODESET);

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

    } catch(Exception e) {
        e.printStackTrace();
    }
}

// Get XSD Expression
private String getExpression(String id) {
    String expression = "";

    switch(id)
    {
    case "99":
        expression = "//xsd:complexType[@name='SomethingOne']//xsd:element";
        break;

    default:
        System.out.println("\n Invalid id");
        break;
    }

    return expression;
}

我遇到的问题是for循环。 result.getLength()返回0。我无法弄清楚为什么。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

你的xsd有效吗?我在线测试了这个表达式并且它有效,但只有你的xsd中有一个小的变通方法。

我只是将代码放在xsd:schema标记中:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified">
    <xsd:complexType name="SomethingOne">
        <xsd:sequence>
            <xsd:element name="Id" type="xsd:int"/>
            <xsd:element name="Time" type="xsd:string"/>
            <xsd:element name="Location" type="xsd:string"/>
            <xsd:element name="Building" type="xsd:string"/>
            <xsd:element name="Comments" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="SomethingTwo">
        <xsd:sequence>
            <xsd:element name="Id" type="xsd:int"/>
            <xsd:element name="Time" type="xsd:string"/>
            <xsd:element name="Location" type="xsd:string"/>
            <xsd:element name="Building" type="xsd:string"/>
            <xsd:element name="Phone" type="xsd:string"/>
            <xsd:element name="Device" type="xsd:string"/>
            <xsd:element name="Protocol" type="xsd:string"/>
            <xsd:element name="Comments" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

对于这些更改,xpath查询似乎没问题。在此测试:http://www.freeformatter.com/xpath-tester.html

<强>更新

删除&#34; xsd:&#34;在xpath查询中:

//complexType[@name='SomethingOne']//element

我在Java项目中测试了它并返回了5个结果。