I'm having trouble validating an XML file in Java, I keep getting the "Invalid type" error. I've been Googling and found some SO threads, which told me to add the namespace in the XML and add "ns:" everywhere, but to no avail.
The XML:
<ns:Content xmlns:ns="http://namespace">
<ns:Request>
<ns:FromDateTime>20130726161606</ns:FromDateTime>
<ns:RemainderOffset/>
<ns:MaxResults>100</ns:MaxResults>
</ns:Request>
<ns:Response>
<ns:Status>
<ns:Code>200</ns:Code>
</ns:Status>
<ns:Remainder>
<ns:Count>2828</ns:Count>
<ns:Offset>126690959</ns:Offset>
</ns:Remainder>
<ns:Items>
<ns:Item id="126752560" itemHash="-1686318559">
<ns:Title>[…]</ns:Title>
<ns:Description>[…]</ns:Description>
<ns:PubDate>Thu, 05 Sep 2013 06:00:27 GMT</ns:PubDate>
<ns:Uri>[…]</ns:Uri>
<ns:Resource>[…]</ns:Resource>
<ns:Keywords>
<ns:Keyword>[…]</ns:Keyword>
</ns:Keywords>
</ns:Item>
</ns:Items>
</ns:Response>
</ns:Content>
The schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://namespace"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Content">
<xs:complexType>
<xs:sequence>
<xs:element name="Request">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:long" name="FromDateTime"/>
<xs:element type="xs:string" name="RemainderOffset"/>
<xs:element type="xs:int" name="MaxResults"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element name="Status">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:short" name="Code"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Remainder">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="Count"/>
<xs:element type="xs:int" name="Offset"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Title"/>
<xs:element type="xs:string" name="Description"/>
<xs:element type="xs:string" name="PubDate"/>
<xs:element type="xs:anyURI" name="Uri"/>
<xs:element type="xs:string" name="Resource"/>
<xs:element name="Keywords">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element type="xs:string" name="Keyword" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:int" name="id" use="required"/>
<xs:attribute type="xs:int" name="itemHash" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
And of course the code I use to read the file. It's a file I download first and it enters the method as Object o
. I think the error is in here somewhere since the XML validates for this schema using online tools... I removed the try-catch blocks for readability.
main method {
File file = (File) o;
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse(file.getAbsolutePath());
doc.getDocumentElement().normalize();
// Add the correct namespace to the xml so there won't be any false positive errors
// Got this part from SO
Element originalDocumentElement = doc.getDocumentElement();
Element newDocumentElement = doc.createElementNS(NAMESPACE, originalDocumentElement.getNodeName());
newDocumentElement.setPrefix("ns");
NodeList list = originalDocumentElement.getChildNodes();
while(list.getLength()!=0) {
newDocumentElement.appendChild(list.item(0));
}
// Replace the original element
doc.replaceChild(newDocumentElement, originalDocumentElement);
xmlOptions = createXmlOptions();
xmlDoc = XmlObject.Factory.parse(doc, xmlOptions);
validate();
}
public XmlOptions createXmlOptions()
{
Map<String, String> substituteNamespaces = new HashMap<String, String>();
substituteNamespaces.put("", "http://namespace");
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setLoadSubstituteNamespaces(substituteNamespaces);
xmlOptions.setCharacterEncoding(FILE_ENCODING);
xmlOptions.setErrorListener(errors);
xmlOptions.setValidateTreatLaxAsSkip();
return xmlOptions;
}
public boolean validate() throws ParserException {
return xmlDoc.validate(xmlOptions);
}
Error message: error: cvc-elt.1: Invalid type
Printing error.getCursorLocation().xmlText()
returns the entire XML file.
The schema is for namespace
targetNamespace="http://red.persgroep.be/technology/1.0"
It has nothing to say about documents in namespace
xmlns:ns="http://namespace"
You can only validate elements against a schema that is applicable to the element's namespace.
<ns:EndDateTime/>
is an EndDateTime
element whose value is the empty string, and I don't think this is valid for an element whose type is xs:long
. You have declared the element both as minOccurs="0"
and nillable="true"
, so you have two choices of ways to represent the absence of an EndDateTime
value - you can either leave the element out altogether or you can mark it as nil using
<ns:EndDateTime xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:nil="true"/>
I still don't know why I got this error, but to anyone facing the same problem: use XML Beans . Very easy to set up and even easier to use. I was able to reuse a lot of my code and it worked instantly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.