在尝试使用XSD验证XML时,今天遇到了一个非常奇怪的问题。 看看我提供的XML,它看起来是正确的。
我从XDocument.Validate收到的错误是: 元素' APPOINTMENTS '有无效的子元素' APPOINTMENT '
以下是我使用的XML:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="PATIENTS">
<xs:complexType>
<xs:sequence>
<xs:element name="PATIENT" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="APPOINTMENTS" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="APPOINTMENT" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="UserInitials" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="Date" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="ApptTime" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="Duration" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="AppointmentStatus" type="xs:string" minOccurs="1"></xs:element>
<xs:element name="LegacyTypeID" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="AClinic" minOccurs="0"></xs:element>
<xs:element name="Notes" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="Info" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="TreatmentType" type="xs:string" minOccurs="0" default="Examination"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我正在验证的XSD文件:
...
new SendUDPTask().execute();
...
/************ getBroadcastAddress: know where to send the signal to find the server ***********/
private InetAddress getBroadcastAddress() {
//setup
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifiManager.getDhcpInfo();
//complicated stuff
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
//return result
InetAddress result = null;
try {
result = InetAddress.getByAddress(quads);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return result;
}
/************ sendUDP: here e send the packet that will allow us to find the server ***********/
private DatagramPacket sendUDP(String req, int port) throws IOException {
DatagramSocket socket = new DatagramSocket(port);
socket.setBroadcast(true);
InetAddress broadcastAddress = getBroadcastAddress();
DatagramPacket packet = new DatagramPacket(req.getBytes(), req.length(), broadcastAddress, port);
socket.send(packet);
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
socket.setSoTimeout(3000);
//We wouldn't want to think our own message is the server's response
String myAddress = getMyAddress();
socket.receive(packet);
while (packet.getAddress().getHostAddress().contains(
myAddress))
{
socket.receive(packet);
}
socket.close();
return packet;
}
/***************************** getMyAddress: the device's address *****************************/
public String getMyAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
return inetAddress.getHostAddress();
}
}
}
catch (SocketException e) {
e.printStackTrace();
}
return null;
}
private class SendUDPTask extends AsyncTask<String, Void, DatagramPacket> {
private Exception exception;
protected DatagramPacket doInBackground(String... params) {
try {
DatagramPacket packet = sendUDP("Ping", 31415);
Log.d("GJ", packet.getAddress().getHostAddress().toString());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(DatagramPacket packet) {
}
}
我不太了解发生了什么,看起来Appointments和Appointments标签符合XSD文件。
除非XSD文件存在问题,否则XML文档的其余部分看起来是正确的。
我的患者元素中有其他元素正常工作。
答案 0 :(得分:0)
我已经解决了这个问题,实际上我错过了部分XML来向您展示。
我应该包含以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<PATIENTS>
<PATIENT>
<APPOINTMENTS>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
<APPOINTMENT>
<UserInitials>123</UserInitials>
<Date>Some Date</Date>
<ApptTime>14:30</ApptTime>
<Duration>00:15</Duration>
<AppointmentStatus>Complete</AppointmentStatus>
<Notes>Some note</Notes>
<TreatmentType>Some Appoinment type</TreatmentType>
</APPOINTMENT>
</APPOINTMENTS>
</PATIENT>
</PATIENTS>
我实际上在APPOINTMENT元素中有多条记录,这需要XSD文件在APPOINTMENT元素上有以下内容:
<xs:element name="APPOINTMENT" minOccurs="0" maxOccurs="unbounded">
我错过了maxOccurs="unbounded"
属性