otto.poellath
otto.poellath

Reputation: 4239

Getting the XSD source from an org.eclipse.xsd.XSDSchema object?

I'm parsing an XML Schema file (*.xsd) using org.eclipse.xsd.XSDSchema and would like to display some XSDTypeDefinitions as literal XSD source.

Here's an example schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="shiporder">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="orderperson" type="xs:string"/>
          <xs:element name="shipto">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
                <xs:element name="city" type="xs:string"/>
                <xs:element name="country" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
      </xs:complexType>
    </xs:element>
</xs:schema>

And here's some code for illustrating the problem:

XSDSchema schema = loadSchemaFromFile(); // not shown
for(XSDElementDeclaration element : schema.getElementDeclarations()){
    assert element.getName().equals("shiporder");

    String xsdSource = // NOW WHAT DO I NEED TO DO HERE?

    String expectedXsdSource = "<xs:element name=\"shiporder\">" +
        "  <xs:complexType>" +
        "    <xs:sequence>" +
        "      <xs:element name=\"orderperson\" type=\"xs:string\"/>" +
        "      <xs:element name=\"shipto\">" +
        "        <xs:complexType>" +
        "          <xs:sequence>" +
        "            <xs:element name=\"address\" type=\"xs:string\"/>" +
        "          </xs:sequence>" +
        "        </xs:complexType>" +
        "      </xs:element>" +
        "  </xs:complexType>" +
        "</xs:element>";

    assert xsdSource.equals(expectedXsdSource);
}

I'm using the following jar files from an Eclipse 3.7 installation:

Upvotes: 1

Views: 519

Answers (1)

erikxiv
erikxiv

Reputation: 4075

Comparing two XML documents using String.equals might be hazardous as white space might differ even though it ought not affect equality.

How about using isEqualNode? You could convert org.eclipse.xsd.XSDSchema to org.w3c.dom.Document by using the getDocument method and the following guide to convert your schema string?

Upvotes: 1

Related Questions