XML DOM parser. Как получить дефолтное значение атрибута?
Есть xsd-схема students.xsd. Атрибут faculty имеет дефолтное значение mmf.
Есть xml-файл students.xml со списком студентов. У одного из студентов не указано название факультета.
Есть класс StudentsDomBuilder с методом buildStudent(). В этом методе устанавливаются значения полей объекта.
Как получить дефолтное значение атрибута faculty, чтобы засетать его объекту?
#students.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/students"
xmlns:tns="http://www.example.com/students"
elementFormDefault="qualified">
<element name="students">
<complexType>
<sequence>
<element name="student" type="tns:Student" minOccurs="1" maxOccurs="100"/>
</sequence>
</complexType>
</element>
<complexType name="Student">
<sequence>
<element name="name" type="string"/>
<element name="telephone" type="positiveInteger"/>
<element name="address" type="tns:Address"/>
</sequence>
<attribute name="login" type="tns:Login" use="required"/>
<attribute name="faculty" use="optional" default="mmf">
<simpleType>
<restriction base="string">
<enumeration value="mmf"></enumeration>
<enumeration value="famcs"></enumeration>
<enumeration value="csan"></enumeration>
</restriction>
</simpleType>
</attribute>
</complexType>
<simpleType name="Login">
<restriction base="ID">
<pattern value="([a-zA-z])[a-zA-z0-9]{7,19}"/>
</restriction>
</simpleType>
<complexType name="Address">
<sequence>
<element name="country" type="string"/>
<element name="city" type="string"/>
<element name="street" type="string"/>
</sequence>
</complexType>
</schema>
#students.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.com/students"
xsi:schemaLocation="http://www.example.com/students students.xsd">
<student login="MitarAlex7">
<name>Mitar Alex</name>
<telephone>2323551</telephone>
<address>
<country>Belarus</country>
<city>Minsk</city>
<street>Kalinouskaha</street>
</address>
</student>
<student faculty="csan" login="Pashkin5" >
<name>Pashkin Jan</name>
<telephone>3453789</telephone>
<address>
<country>Belarus</country>
<city>Polotsk</city>
<street>Bahdanovicha</street>
</address>
</student>
</students>
#StudentsDomBuilder
public class StudentsDomBuilder {
static Logger logger = LogManager.getLogger();
private Set<Student> students;
private DocumentBuilder docBuilder;
public StudentsDomBuilder() {
students = new HashSet<>();
//configuration
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
docBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
logger.error("Doc builder is not created.");
}
}
public Set<Student> getStudents() {
return students;
}
public void buildSetStudents(String filename) {
Document doc;
try {
doc = docBuilder.parse(filename);
Element root = doc.getDocumentElement();
//getting a list of <student> child elements
NodeList studentsList = root.getElementsByTagName("student");
for (int i = 0; i < studentsList.getLength(); i++) {
Element studentElement = (Element) studentsList.item(i);
Student student = buildStudent(studentElement);
students.add(student);
}
} catch (IOException | SAXException e) {
logger.error("Students set is not built.");
}
}
private Student buildStudent(Element studentElement) {
Student student = new Student();
if (studentElement.hasAttribute("faculty")) {
student.setFaculty(studentElement.getAttribute("faculty"));
} else {
//TODO set default value
student.setFaculty("mmf");
}
student.setName(getElementTextContent(studentElement, "name"));
int tel = Integer.parseInt(getElementTextContent(studentElement, "telephone"));
student.setTelephone(tel);
Student.Address address = student.getAddress();
//init an address object
Element addressElement = (Element) studentElement.getElementsByTagName("address").item(0);
address.setCountry(getElementTextContent(addressElement, "country"));
address.setCity(getElementTextContent(addressElement, "city"));
address.setStreet(getElementTextContent(addressElement, "street"));
student.setLogin(studentElement.getAttribute("login"));
return student;
}
//get the text content of the tag
private String getElementTextContent(Element element, String elementName) {
NodeList nList = element.getElementsByTagName(elementName);
Node node = nList.item(0);
return node.getTextContent();
}
}