XML / String-Problem

  • Antworten:3
  • Bentwortet
Markus K.
  • Forum-Beiträge: 24

03.01.2012, 16:51:07 via Website

Hi Leute,
ich lasse in meiner App Daten anzeigen, die ich von einer Web-Schnittstelle erhalte. Das klappt auch soweit ganz prima.

An einer Stelle steht jedoch nen Teil einer URL drin, mit dem ich nix anfangen kann.

<PARAMETER>KTFDID=12345&amp;showresult=1</PARAMETER>

Wenn ich den Wert auslese bekomme ich nur "KTFDID=12345" zurück....


Der Code, den ich verwende sieht in etwa so aus:
1public class XMLfunctions {
2
3 public final static Document XMLfromString(String xml){
4
5 Document doc = null;
6
7 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
8 try {
9
10 DocumentBuilder db = dbf.newDocumentBuilder();
11
12 InputSource is = new InputSource();
13 is.setCharacterStream(new StringReader(xml));
14 doc = db.parse(is);
15
16 } catch (ParserConfigurationException e) {
17 System.out.println("XML parse error: " + e.getMessage());
18 return null;
19 } catch (SAXException e) {
20 System.out.println("Wrong XML file structure: " + e.getMessage());
21 return null;
22 } catch (IOException e) {
23 System.out.println("I/O exeption: " + e.getMessage());
24 return null;
25 }
26
27 return doc;
28
29 }
30
31 /** Returns element value
32 * @param elem element (it is XML tag)
33 * @return Element value otherwise empty String
34 */
35 public final static String getElementValue( Node elem ) {
36 Node kid;
37 if( elem != null){
38 if (elem.hasChildNodes()){
39 for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
40 if( kid.getNodeType() == Node.TEXT_NODE ){
41 return kid.getNodeValue();
42 }
43 }
44 }
45 }
46 return "";
47 }
48
49 public static String getXML(){
50 String line = null;
51
52 try {
53
54 DefaultHttpClient httpClient = new DefaultHttpClient();
55 HttpPost httpPost = new HttpPost("http://....");
56
57 HttpResponse httpResponse = httpClient.execute(httpPost);
58 HttpEntity httpEntity = httpResponse.getEntity();
59 line = EntityUtils.toString(httpEntity);
60
61 } catch (UnsupportedEncodingException e) {
62 line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
63 } catch (MalformedURLException e) {
64 line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
65 } catch (IOException e) {
66 line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
67 }
68
69 return line;
70
71 }
72
73 public static int numResults(Document doc){
74 Node results = doc.getDocumentElement();
75 int res = -1;
76
77 try{
78 res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
79 }catch(Exception e ){
80 res = -1;
81 }
82
83 return res;
84 }
85
86 public static String getValue(Element item, String str) {
87 NodeList n = item.getElementsByTagName(str);
88 return XMLfunctions.getElementValue(n.item(0));
89 }
90}

Nach langem Googeln und ausprobieren bin ich nun völlig ratlos, deshalb hoffe ich auf eure Hilfe.

gruß
Markus

Antworten
Felix
  • Forum-Beiträge: 259

03.01.2012, 17:42:09 via Website

Tach!

An welcher Stelle des Codes verschwindet denn der zweite Teil? Du kannst das doch sicher mit dem Debugger laufen lassen und dann genau die Handvoll problematische Zeilen zeigen, wenn du dann immer noch nicht verstehst, wie das Problem zustande kommt.


Felix.

Antworten
Markus K.
  • Forum-Beiträge: 24

04.01.2012, 11:01:31 via Website

Danke, hat sich jetzt erledigt :)

Antworten