org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog

Schuriko

Bekanntes Mitglied
ich möchte mir von OpenLigaDB über https://www.openligadb.de/api/getbltable/bl1/2019 eine XML Document abholen.

Meine Spring Boot Anwendung sieht hierbei wie folgt aus:
Code:
@SpringBootApplication
public class TipApplication {
     private static Document loadTestDocument(String url) throws Exception {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            return factory.newDocumentBuilder().parse(new URL(url).openStream());
        }
    
    public static void main(String[] args) throws Exception {
        Document  doc = loadTestDocument("https://www.openligadb.de/api/getbltable/bl1/2019");
        System.out.println(doc);
            
        SpringApplication.run(TipApplication.class, args);
    }

}

Führe ich die Anwendung aus, dann wir mir gesagt:
[Fatal Error] :1:1: Content is not allowed in prolog.
Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:261)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:122)
at com.schuriko.tip.TipApplication.loadTestDocument(TipApplication.java:15)
at com.schuriko.tip.TipApplication.main(TipApplication.java:19)

Was mache ich hier gerade falsch?
 

mihe7

Top Contributor
Der Server liefert nicht automatisch XML sondern JSON. Probier mal etwas wie
Java:
URLConnection conn = new URL(url).openConnection();
conn.setRequestPropery("Accept", "application/xml");
return factory.newDocumentBuilder().parse(conn.getInputStream());
 

Schuriko

Bekanntes Mitglied
Der Server liefert nicht automatisch XML sondern JSON. Probier mal etwas wie
Java:
URLConnection conn = new URL(url).openConnection();
conn.setRequestPropery("Accept", "application/xml");
return factory.newDocumentBuilder().parse(conn.getInputStream());
Ich habe meine Sachen mal nach deinem Vorschlag geändert:
Code:
public class TipApplication {
     private static Document loadTestDocument(String url) throws Exception {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);

/*         
            return factory.newDocumentBuilder().parse(new URL(url).openStream());
*/
         URLConnection conn = new URL(url).openConnection();
         conn.setRequestProperty("Accept", "application/xml");
         return factory.newDocumentBuilder().parse(conn.getInputStream());         
        }
    
    public static void main(String[] args) throws Exception {
        Document  doc = loadTestDocument("https://www.openligadb.de/api/getbltable/bl1/2019");
        System.out.println(doc);
            
        SpringApplication.run(TipApplication.class, args);
    }
    

}

Ok, er liefert mir keinen Fehler mehr, aber auch nicht das gewünscht. Ausgabe ist
[#document: null]
[#document: null]

Rufe ich aber über einen Browser https://www.openligadb.de/api/getbltable/bl1/2019 ab erhalte ich
Code:
<ArrayOfBlTableTeam xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/OLDB.Spa.Models.Api">
<BlTableTeam>
<Draw>4</Draw>
<Goals>73</Goals>
<Lost>4</Lost>
<Matches>25</Matches>
<OpponentGoals>26</OpponentGoals>
<Points>55</Points>
<ShortName>FC Bayern</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Logo_FC_Bayern_M%C3%BCnchen_%282002%E2%80%932017%29.svg/240px-Logo_FC_Bayern_M%C3%BCnchen_%282002%E2%80%932017%29.svg.png
</TeamIconUrl>
<TeamInfoId>40</TeamInfoId>
<TeamName>FC Bayern</TeamName>
<Won>17</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>9</Draw>
<Goals>62</Goals>
<Lost>3</Lost>
<Matches>26</Matches>
<OpponentGoals>26</OpponentGoals>
<Points>51</Points>
<ShortName>RBL Leipzig</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/en/thumb/0/04/RB_Leipzig_2014_logo.svg/800px-RB_Leipzig_2014_logo.svg.png
</TeamIconUrl>
<TeamInfoId>1635</TeamInfoId>
<TeamName>RB Leipzig</TeamName>
<Won>14</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>6</Draw>
<Goals>68</Goals>
<Lost>4</Lost>
<Matches>25</Matches>
<OpponentGoals>33</OpponentGoals>
<Points>51</Points>
<ShortName>Dortmund</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/Borussia_Dortmund_logo.svg/240px-Borussia_Dortmund_logo.svg.png
</TeamIconUrl>
<TeamInfoId>7</TeamInfoId>
<TeamName>Borussia Dortmund</TeamName>
<Won>15</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>4</Draw>
<Goals>49</Goals>
<Lost>6</Lost>
<Matches>25</Matches>
<OpponentGoals>30</OpponentGoals>
<Points>49</Points>
<ShortName>Gladbach</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/8/81/Borussia_Mönchengladbach_logo.svg
</TeamIconUrl>
<TeamInfoId>87</TeamInfoId>
<TeamName>Borussia Mönchengladbach</TeamName>
<Won>15</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>5</Draw>
<Goals>45</Goals>
<Lost>6</Lost>
<Matches>25</Matches>
<OpponentGoals>30</OpponentGoals>
<Points>47</Points>
<ShortName>Leverkusen</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/de/thumb/f/f7/Bayer_Leverkusen_Logo.svg/1280px-Bayer_Leverkusen_Logo.svg.png
</TeamIconUrl>
<TeamInfoId>6</TeamInfoId>
<TeamName>Bayer Leverkusen</TeamName>
<Won>14</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>10</Draw>
<Goals>33</Goals>
<Lost>6</Lost>
<Matches>25</Matches>
<OpponentGoals>36</OpponentGoals>
<Points>37</Points>
<ShortName>Schalke 04</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/6/6d/FC_Schalke_04_Logo.svg
</TeamIconUrl>
<TeamInfoId>9</TeamInfoId>
<TeamName>FC Schalke 04</TeamName>
<Won>9</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>9</Draw>
<Goals>34</Goals>
<Lost>7</Lost>
<Matches>25</Matches>
<OpponentGoals>30</OpponentGoals>
<Points>36</Points>
<ShortName>Wolfsburg</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Logo-VfL-Wolfsburg.svg/1024px-Logo-VfL-Wolfsburg.svg.png
</TeamIconUrl>
<TeamInfoId>131</TeamInfoId>
<TeamName>VfL Wolfsburg</TeamName>
<Won>9</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>6</Draw>
<Goals>34</Goals>
<Lost>9</Lost>
<Matches>25</Matches>
<OpponentGoals>35</OpponentGoals>
<Points>36</Points>
<ShortName>SC Freiburg</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/de/thumb/f/f1/SC-Freiburg_Logo-neu.svg/739px-SC-Freiburg_Logo-neu.svg.png
</TeamIconUrl>
<TeamInfoId>112</TeamInfoId>
<TeamName>SC Freiburg</TeamName>
<Won>10</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>5</Draw>
<Goals>35</Goals>
<Lost>10</Lost>
<Matches>25</Matches>
<OpponentGoals>43</OpponentGoals>
<Points>35</Points>
<ShortName>Hoffenheim</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/6/64/TSG_Logo-Standard_4c.png
</TeamIconUrl>
<TeamInfoId>175</TeamInfoId>
<TeamName>TSG 1899 Hoffenheim</TeamName>
<Won>10</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>2</Draw>
<Goals>39</Goals>
<Lost>13</Lost>
<Matches>25</Matches>
<OpponentGoals>45</OpponentGoals>
<Points>32</Points>
<ShortName>1. FC Köln</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/en/thumb/5/53/FC_Cologne_logo.svg/901px-FC_Cologne_logo.svg.png
</TeamIconUrl>
<TeamInfoId>65</TeamInfoId>
<TeamName>1. FC Köln</TeamName>
<Won>10</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>3</Draw>
<Goals>32</Goals>
<Lost>13</Lost>
<Matches>25</Matches>
<OpponentGoals>41</OpponentGoals>
<Points>30</Points>
<ShortName>Union Berlin</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/1._FC_Union_Berlin_1966_-_1990.gif/320px-1._FC_Union_Berlin_1966_-_1990.gif
</TeamIconUrl>
<TeamInfoId>80</TeamInfoId>
<TeamName>1. FC Union Berlin</TeamName>
<Won>9</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>4</Draw>
<Goals>38</Goals>
<Lost>12</Lost>
<Matches>24</Matches>
<OpponentGoals>41</OpponentGoals>
<Points>28</Points>
<ShortName>Frankfurt</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Eintracht_Frankfurt_Logo.svg/1024px-Eintracht_Frankfurt_Logo.svg.png
</TeamIconUrl>
<TeamInfoId>91</TeamInfoId>
<TeamName>Eintracht Frankfurt</TeamName>
<Won>8</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>7</Draw>
<Goals>32</Goals>
<Lost>11</Lost>
<Matches>25</Matches>
<OpponentGoals>48</OpponentGoals>
<Points>28</Points>
<ShortName>Hertha BSC</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Hertha_BSC_Logo_2012.svg/2858px-Hertha_BSC_Logo_2012.svg.png
</TeamIconUrl>
<TeamInfoId>54</TeamInfoId>
<TeamName>Hertha BSC</TeamName>
<Won>7</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>6</Draw>
<Goals>36</Goals>
<Lost>12</Lost>
<Matches>25</Matches>
<OpponentGoals>52</OpponentGoals>
<Points>27</Points>
<ShortName>Augsburg</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/de/b/b5/Logo_FC_Augsburg.svg
</TeamIconUrl>
<TeamInfoId>95</TeamInfoId>
<TeamName>FC Augsburg</TeamName>
<Won>7</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>3</Draw>
<Goals>34</Goals>
<Lost>15</Lost>
<Matches>26</Matches>
<OpponentGoals>53</OpponentGoals>
<Points>27</Points>
<ShortName>FSV Mainz</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Logo_Mainz_05.svg/1099px-Logo_Mainz_05.svg.png
</TeamIconUrl>
<TeamInfoId>81</TeamInfoId>
<TeamName>1. FSV Mainz 05</TeamName>
<Won>8</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>7</Draw>
<Goals>27</Goals>
<Lost>13</Lost>
<Matches>25</Matches>
<OpponentGoals>50</OpponentGoals>
<Points>22</Points>
<ShortName>Düsseldorf</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Fortuna_D%C3%BCsseldorf.svg/150px-Fortuna_D%C3%BCsseldorf.svg.png
</TeamIconUrl>
<TeamInfoId>185</TeamInfoId>
<TeamName>Fortuna Düsseldorf</TeamName>
<Won>5</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>6</Draw>
<Goals>27</Goals>
<Lost>14</Lost>
<Matches>24</Matches>
<OpponentGoals>55</OpponentGoals>
<Points>18</Points>
<ShortName>Bremen</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/SV-Werder-Bremen-Logo.svg/681px-SV-Werder-Bremen-Logo.svg.png
</TeamIconUrl>
<TeamInfoId>134</TeamInfoId>
<TeamName>Werder Bremen</TeamName>
<Won>4</Won>
</BlTableTeam>
<BlTableTeam>
<Draw>4</Draw>
<Goals>30</Goals>
<Lost>17</Lost>
<Matches>25</Matches>
<OpponentGoals>54</OpponentGoals>
<Points>16</Points>
<ShortName>Paderborn</ShortName>
<TeamIconUrl>
https://upload.wikimedia.org/wikipedia/commons/e/e3/SC_Paderborn_07_Logo.svg
</TeamIconUrl>
<TeamInfoId>31</TeamInfoId>
<TeamName>SC Paderborn 07</TeamName>
<Won>4</Won>
</BlTableTeam>
Also liefern tut die OpenLigaDB etwas, aber er scheint es nicht übersetzen zu können. Was mach ich falsch?
 

LimDul

Top Contributor
XML Parsing hab ich schon länger nicht mehr gemacht, mein Vorgehen wäre:
* Schau mal, ob der Inputstream wirklich das XML liefert (also ob in conn.getInputStream() wirklich das XML drin steckt)
* Brauchst du das NameSpaceAware? (Wie gesagt meine XML-Kenntnisse sind eingerostet)
 

Flown

Administrator
Mitarbeiter
Also du kannst nicht einfach so das Document auf String ausgeben:
Java:
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URLConnection conn = new URL("https://www.openligadb.de/api/getbltable/bl1/2019").openConnection();
conn.setRequestProperty("Accept", "application/xml");
Document document = documentBuilder.parse(conn.getInputStream());

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
 

Neue Themen


Oben