ERLEDIGT
JA
JA
ANTWORTEN
2
2
ZUGRIFFE
575
575
EMPFEHLEN
-
16.09.11 10:11 #1
- Registriert seit
- Sep 2011
- Beiträge
- 2
Hey!
Ich bekomme immer eine Fehlermeldung, wenn ich versuche meine XML-Datei zu validieren:
convert.xml is not valid because
cvc-elt.1: Cannot find the declaration of element 'input'.
input ist mein root-element in der XML-Datei und ist auch in der XSD-Datei so angegeben. Daher frage ich mich, warum er es nicht finden kann.
also meine Schema-Datei "input.xsd" sieht so aus:
Code :1 2 3 4 5 6 7 8
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/input" xmlns:tns="http://www.example.org/input" elementFormDefault="qualified"> <xsd:element name="input" type="tns:inputType"></xsd:element> ...hier stehen noch Type-Definitionen... </xsd:schema>
Meine XML-Datei "convert.xml" sieht so aus:
Code :1 2 3 4 5 6 7 8
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- <input xmlns="http://www.example.org/input/"> --> <input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="input.xsd"> <convert todo="kml to dff"> <inputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.kml</inputFile> <outputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.dff</outputFile> </convert> </input>
den auskommentierten Teil in der 2. Zeile hatte ich mal als Alternative zu dem root-element getestet, hat aber auch nichts gebracht...
und meine Java-Klasse, die validieren soll (wobei file der XML-Datei convert.xml entspricht):
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// 1. Lookup a factory for the W3C XML Schema language SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); // 2. Compile the schema. // Here the schema is loaded from a java.io.File, but you could use // a java.net.URL or a javax.xml.transform.Source instead. File schemaLocation = new File("input.xsd"); Schema schema = null; try { schema = factory.newSchema(schemaLocation); } catch (SAXException e1) { System.out.println(e1.getMessage()); } // 3. Get a validator from the schema. Validator validator = schema.newValidator(); // 4. Parse the document you want to check. Source source = new StreamSource(file); // 5. Check the document try { validator.validate(source); System.out.println(file + " is valid."); }catch (SAXException ex) { System.out.println(file + " is not valid because "); System.out.println(ex.getMessage()); System.exit(1); } catch (IOException e) { System.out.println(e.getMessage()); }
Ich habe das ganze dann auch nochmal auf einem anderen Weg versucht:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// create a factory that understands namespaces and validates the XML input DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // read the XML file DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e3) { e3.printStackTrace(); } Document doc = null; try { doc = builder.parse(new File("convert.xml")); } catch (SAXException e2) { e2.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } // create a SchemaFactory and a Schema SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(new File("input.xsd")); Schema schema = null; try { schema = schemaFactory.newSchema(schemaFile); } catch (SAXException e1) { e1.printStackTrace(); } // create a Validator object and validate the XML file Validator validator = schema.newValidator(); try { validator.validate(new DOMSource(doc)); System.out.println(file + " is valid"); } catch (SAXException e) { System.out.println(file + " is not valid because: "); System.out.println(e.getMessage()); } catch (IOException e) { e.printStackTrace(); }
...ging aber natürlich auch nicht -> gleicher Fehler...
Hat jemand eine Idee, wo hier der Fehler liegen könnte? Ich hab schon von vielen ähnlichen Problemen und auch Lösungen gelesen und auch schon viel mit den Namespaces herumprobiert, aber bei mir möchte einfach nichts funktionieren...
Wäre echt super, wenn mir jemand helfen könnte!
Danke schonmal, SarahGeändert von blauer_hase (16.09.11 um 10:33 Uhr)
-
16.09.11 12:39 #2
- Registriert seit
- Sep 2011
- Beiträge
- 2
wow, ich glaub ich habs gelöst... hab so lange rumprobiert bis jetzt plötzlich die XML-Datei valid war...
Also für alle anderen, die das Problem auch haben:
Ich habe den Namespace xsd: aus dem Schema herausgenommen und das root-element umbenannt, da es genauso hieß wie auch das Schema (keine Ahnung, ob das was mit dem Fehler zu tun hat, jetzt geht es jedenfalls, dann lass ich jetzt lieber die Finger von weiteren Spielereien):
Code :1 2 3 4 5 6 7 8 9
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/input/" xmlns:tns="http://www.example.org/input/" elementFormDefault="qualified"> <element name="root" type="tns:rootType"></element> ...Type-Definitionen... </schema>
und auch in der XML-Datei habe ich die auskommentierte Alternative für das root-element benutzt und natürlich auch hier den Namen des root-element entsprechend des Schemas geändert:
Code :1 2 3 4 5 6 7
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <root xmlns="http://www.example.org/input/"> <convert todo="kml to dff"> <inputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.kml</inputFile> <outputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.dff</outputFile> </convert> </root>
zusammen mit diesem Java-Code funktionierte es dann:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// 1. Lookup a factory for the W3C XML Schema language SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); // 2. Compile the schema. // Here the schema is loaded from a java.io.File, but you could use // a java.net.URL or a javax.xml.transform.Source instead. File schemaLocation = new File("input.xsd"); Schema schema = null; try { schema = factory.newSchema(schemaLocation); } catch (SAXException e1) { System.out.println(e1.getMessage()); } // 3. Get a validator from the schema. Validator validator = schema.newValidator(); // 4. Parse the document you want to check. Source source = new StreamSource(file); // 5. Check the document try { validator.validate(source); System.out.println(file + " is valid."); }catch (SAXException ex) { System.out.println(file + " is not valid because "); System.out.println(ex.getMessage()); System.exit(1); } catch (IOException e) { System.out.println(e.getMessage()); }
Also ingesamt hab ich geändert: den Namen des root-elements, den Namespace xsd: rausgenommen und dieses root-element benutzt: <root xmlns="http://www.example.org/input/">
-
Wenn du die Lösung hast, dann setzt das Thema auf erledigt

Aber hier noch nee Erklärung:
Erstmal ist für den Parser die XML nur eine simple text datei und wird so gelesen. Anhand von Keywords und regex werden Elemente und co erkannt und vom Parser Verarbeitet, du hast nun ein Keyword verwendet welches intern schon verwendet wird und dein Validator hat sich daran verschluckt
MFG
Ähnliche Themen
-
XML-Datei nach XML-Schema validieren
Von Tikonteroga im Forum XML TechnologienAntworten: 5Letzter Beitrag: 24.01.12, 17:44 -
Validierung gegen Schema mittels DOM
Von schlachtrufe im Forum JavaAntworten: 2Letzter Beitrag: 22.02.08, 01:33 -
XML Dateien gegen Schema validieren und in Ordner verschieben
Von gatsneid im Forum XML TechnologienAntworten: 2Letzter Beitrag: 29.08.07, 15:46 -
Mit JDOM XML-Datei gegen XML-Schema validieren
Von mmueller78 im Forum JavaAntworten: 1Letzter Beitrag: 17.08.07, 14:15 -
XML gegen Schema validieren
Von mtk-flo im Forum Visual Basic 6.0Antworten: 2Letzter Beitrag: 27.06.07, 16:02





Zitieren
Login





