j'arrive pas a lire un fichier xml coder en UTF-8, j'ai esseyé mais ca me donne toujours l'erreur "Exception in thread "main" com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence."
j'ai chercher sur internet ils disent que le probléme est dans l'encoding UTF-8
et qu'il faut la changer en ISO-8859-1, alors j'ai fait un ptit script qui prend la ligne (en String) ou il est écrit UTF-8 et il la change en ISO-8859-1, mais ca me donne cette erreur "An invalid XML character (Unicode: 0x1d) was found in the element content of the document.", mais quand c'est moi qui modifie UTF-8 en ISO-8859-1 dans le fichier XML ca marche super, la je pense que c'est juste un probléme dans la sérialisation du fichier (je pense).
Mais malgré ca moi ce que je veux qu'il lit le fichier meme si c'est UTF-8
voila mon Code
c'est a peut prés le code, si vous connaissez un autre moyen plus facile dite le moi; je veux que mon programme telecharge les données d'une URL qui est en XML le met dans un fichier et qu'avec une autre méthode qu'il puisse lire le fichier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 import java.io.*; import org.w3c.dom.Document; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class EditFile{ public EditFile(String file){ docBuilderFactory =DocumentBuilderFactory.newInstance(); docBuilder = docBuilderFactory.newDocumentBuilder(); doc = docBuilder.parse (new File(file)); doc.getDocumentElement().normalize(); } public void GetURL(String url)throws Exception{ EditFile out = new EditFile("URLContent.xml"); URL yahoo = new URL(url); URLConnection yc = yahoo.openConnection(); yc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.19)"); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine; try { while ((inputLine = in.readLine()) != null){ out.Write(inputLine); } } finally { out.close(); } in.close(); } public String[][] ReadXML(String Item, String[] Tags ) { NodeList listOfPersons = doc.getElementsByTagName(Item); String[][] TagsContenu = new String[listOfPersons.getLength()][Tags.length]; for(int s=0; s<listOfPersons.getLength() ; s++){ Node firstPersonNode = listOfPersons.item(s); if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){ Element firstPersonElement = (Element)firstPersonNode; for(int i=0; i<Tags.length; i++){ NodeList firstNameList = firstPersonElement.getElementsByTagName(Tags[i]); Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); TagsContenu[s][i] = (String)((Node)textFNList.item(0)).getNodeValue().trim(); } }//end of if clause }//end of for loop with s var return TagsContenu; } public void Write(String topic) throws FileNotFoundException, IOException{ output.seek(output.length()); if(topic.indexOf("UTF-8") == -1) output.writeBytes(topic); else{ topic = topic.replaceFirst("UTF-8", "ISO-8859-1"); output.writeBytes(topic); } output.seek(output.length()); } public static void main(String[] args) throws Exception{ Connect get = new Connect(); get.GetURL("http://google.com"); EditFile XMLEdit = new EditFile("URLContent.xml", 'r'); String[] tag = {"title", "content", "id", "updated"}; String[][] Entry = XMLEdit.ReadXML("entry", tag); for(int i=0; i<Entry.length; i++) for(int j=0; j<Entry[i].length; j++) System.out.println(Entry[i][j]+"\n"); } }
Partager