Mark Mamdouh
Mark Mamdouh

Reputation: 169

Validate XML file in java

I have the following xml :

<Shapes>
    <Numbers>n-2</Numbers>
    <Triangle.0>
        <Color.0>
            <Red>r-0</Red>
            <Green>g-0</Green>
            <Blue>b-0</Blue>
        </Color.0>
        <FillColor.0>
            <Red>r-0</Red>
            <Green>g-0</Green>
            <Blue>b-0</Blue>
        </FillColor.0>
        <Position.0>
            <X>x-862.0</X>
            <Y>y-333.0</Y>
        </Position.0>
        <propertiesNumber.0>p-4</propertiesNumber.0>
        <properties.0>
            <PointX-b>v-0.0</PointX-b>
            <PointY-b>v-0.0</PointY-b>
            <PointX-a>v-100.0</PointX-a>
            <PointY-a>v-100.0</PointY-a>
        </properties.0>
    </Triangle.0>
</Shapes>

and i want to validate it using DTD in java.

and i wrote this schema :

<!ELEMENT Shapes (Numbers, Triangle.0)>
<!ELEMENT Numbers (#PCDATA)>
<!ELEMENT Triangle.0 (Color.0, FillColor.0, Position.0, propertiesNumber.0,      properties.0)>
<!ELEMENT Color.0 (Red, Green, Blue)>
<!ELEMENT Red (#PCDATA)>
<!ELEMENT Green (#PCDATA)>
<!ELEMENT Blue (#PCDATA)>
<!ELEMENT FillColor.0 (Red, Green, Blue)>
<!ELEMENT Red (#PCDATA)>
<!ELEMENT Green (#PCDATA)>
<!ELEMENT Blue (#PCDATA)>
<!ELEMENT Position.0 (X, Y)>
<!ELEMENT X (#PCDATA)>
<!ELEMENT Y (#PCDATA)>
<!ELEMENT propertiesNumber.0 (#PCDATA)>
<!ELEMENT properties.0 (PointX-b, PointY-b, PointX-a, PointY-a)>
<!ELEMENT PointX-b (#PCDATA)>
<!ELEMENT PointY-b (#PCDATA)>
<!ELEMENT PointX-a (#PCDATA)>
<!ELEMENT PointY-a (#PCDATA)>

but it gives me error that elements Red, Green, Blue must not be declared more than once. what can i do?

Upvotes: 1

Views: 118

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

Remove the extra declarations for Red, Green, and Blue. They only need to be declared once.

Also, don't include the <!DOCTYPE declaration at the end of your DTD. (Not sure if that's a typo.)

Third, your XML still won't validate against your DTD because Rectangle.1 is required. Either make it optional in the dtd or add it to your XML. If you add it to your XML, you will also need to declare it in your DTD.

Upvotes: 1

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31279

The error, which you paraphrased as "elements Red, Green, Blue must not be declared more than once" is pretty clear. In your DTD, you have declared elements Red, Green and Blue more than once:

<!ELEMENT Red (#PCDATA)>
<!ELEMENT Green (#PCDATA)>
<!ELEMENT Blue (#PCDATA)>

This section occurs twice in your DTD.

Remove one of these occurrences and you should be rid of this error.

Upvotes: 2

Related Questions