probleme beim xml parsen

  • Antworten:1
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

10.05.2013, 13:07:24 via Website

Hallo,
ich habe ein Problem beim parsen einer XML.
Meíne XML:

1<xml>
2<urls>
3<url name="eintrag1">http://www.url1.de</url>
4<url name="eintrag2">http://www.url2.de</url>
5<url name="eintrag3">http://www.url3.de</url>
6<url name="eintrag4">http://www.url4.de</url>
7<url name="eintrag5">http://www.url5.de</url>
8<url name="eintrag6">http://www.url6.de</url>
9<url name="eintrag7">http://www.url7.de</url>
10</urls>
11</xml>

Ich will alle fettgedruckten Werte auslesen.
Bisher habe sich es nur geschafft, die Eintragsnummer auszulesen.

Mein Code:
XML Funktions.java
:
1public class XMLFunctions {
2public final static Document XMLfromString(String xml){
3
4 Document doc = null;
5
6 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
7 try {
8
9 DocumentBuilder db = dbf.newDocumentBuilder();
10
11 InputSource is = new InputSource();
12 is.setCharacterStream(new StringReader(xml));
13 doc = db.parse(is);
14
15 } catch (ParserConfigurationException e) {
16 System.out.println("XML parse error: " + e.getMessage());
17 return null;
18 } catch (SAXException e) {
19 System.out.println("Wrong XML file structure: " + e.getMessage());
20 return null;
21 } catch (IOException e) {
22 System.out.println("I/O exeption: " + e.getMessage());
23 return null;
24 }
25
26 return doc;
27
28 }
29
30 /** Returns element value
31 * @param elem element (it is XML tag)
32 * @return Element value otherwise empty String
33 */
34 public final static String getElementValue( Node elem ) {
35 Node kid;
36 if( elem != null){
37 if (elem.hasChildNodes()){
38 for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
39 if( kid.getNodeType() == Node.TEXT_NODE ){
40 return kid.getNodeValue();
41 }
42 }
43 }
44 }
45 return "";
46 }
47
48 volatile static String toReturn;
49
50
51 public static String getXML(String url){
52
53
54 http http = new http();
55
56 toReturn = http.get(url);
57
58 return toReturn;
59
60
61
62 }
63
64 public static int numResults(Document doc){
65 Node results = doc.getDocumentElement();
66 int res = -1;
67
68 try{
69 res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
70 }catch(Exception e ){
71 res = -1;
72 }
73
74 return res;
75 }
76
77 public static String getValue(Element item, String str) {
78 NodeList n = item.getElementsByTagName(str);
79 return XMLFunctions.getElementValue(n.item(0));
80 }
81
82}

und meine XML.java

1if(xml == null)
2 {
3
4 xml = XMLFunctions.getXML("urlToXML");
5
6 }
7
8
9
10
11
12 if(mylist.isEmpty() || mylist == null){
13
14 Document doc = XMLFunctions.XMLfromString(xml);
15
16 int numResults = XMLFunctions.numResults(doc);
17
18
19
20 NodeList nodes = doc.getElementsByTagName("url");
21
22
23 for (int i = 0; i < nodes.getLength(); i++) {
24 HashMap<String, String> map = new HashMap<String, String>();
25
26 Element e = (Element)nodes.item(i);
27
28 String Eintragnr = String.valueOf(e.getAttributes().getNamedItem("name").getNodeValue()));
29
30 mylist.add(map);
31
32
33 }}
34 }
So hab ich es gemacht. Nun will ich auch die eigentlichen Values auslesen.
Dafür gibt es in den XMLFunktions eine Funktion "getValue(Element,String)".
Bei anderen XML Dateien funktionier das gut. Bei dieser nicht.

Der Befehl zum auslesen der Values müsste "XMLFunctions.getValue(e, "url")" lauten.
Aber wenn ich versuche den Sting auszulesen bleibt er leer.
Hat jemand eine Idee oder einen Tipp.
Ich würde mich Freuen

Vielen Dank im Voraus

Pascal

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

11.05.2013, 13:29:14 via Website

Ich hab das Problem gelöst :) :lol:
Code:
1String xml = XMLFunctions.getXML("urlToScript");
2 Document doc = XMLFunctions.XMLfromString(xml);
3
4
5 NodeList nodes = doc.getElementsByTagName("url");
6
7 for (int i = 0; i < (nodes.getLength()); i++) {
8 HashMap<String, String> map = new HashMap<String, String>();
9
10 Element e = (Element)nodes.item(i);
11
12
13 String value = null;
14 if(e.hasChildNodes())
15 {
16
17 value = e.getFirstChild().getNodeValue().toString(); // URL
18 }
19
20 String attribute = null;
21 if(e.hasAttributes())
22 {
23 if(e.hasAttribute("name"))
24 {
25 attribute = String.valueOf(e.getAttributes().getNamedItem("name").getNodeValue());´// Eintragsnummer
26 }
27 }

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten