0

Following is my problem statement:

XYZ school wants to store the details of student and staff in a xml file. The following scenario helps in designing the XML document.persons will be the root tag. persons will havethe entry of each person with name, age, gender, address. A person can be either a student or staff. Student will have rollno, standard and section. If staff, then staffid and subject. Every person must have an address with the following entry - doorno,street,city and state.

The code which I wrote is as follows:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE persons
[
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT address (doorno,street,city,state)>
<!ELEMENT student (rollno,standard,section)>
<!ELEMENT rollno (#PCDATA)>
<!ELEMENT standard (#PCDATA)>
<!ELEMENT section (#PCDATA)>
<!ELEMENT staff (staffid,subject)>
<!ELEMENT staffid (#PCDATA)>
<!ELEMENT subject (#PCDATA)>
<!ELEMENT doorno (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>

]>

It gives the error- Exception:Premature end of file. I am quite new at XML and hence having a hard time. Help would be appreciated

tron042
  • 49
  • 8

1 Answers1

0

I'm guessing whatever you are using to check your document is complaining that it is not well-formed as it hasn't got a root node. In your case, I expect the root node should be persons, so adding <persons/> will give you a well-formed document, though it won't yet be valid.

You can use a validation tool like xmllint to check for validity. See Is there a difference between 'valid xml' and 'well formed xml'?

Although you have specified that the root node of the DTD is persons, you haven't actually defined the persons or person elements. An example might set you on the right path. https://www.w3schools.com/xml/xml_dtd_intro.asp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person [
<!ELEMENT person (student | staff)>
<!ELEMENT staff EMPTY>
<!ELEMENT student EMPTY>
<!ATTLIST person name CDATA #REQUIRED age CDATA #REQUIRED>
<!ATTLIST student rollno CDATA #REQUIRED>
]>
<person age="1" name="A">
<student rollno="20013456"/>
</person>
Mic
  • 331
  • 1
  • 4