网上许多例子为简单起见,大多都是简单的XML文件。
下面两篇文章供初学者参考,文章解释得比较清楚。
http://blog.csdn.net/zzp16/article/details/7795410
http://blog.csdn.net/liuhe688/article/details/6415593
初学者对照例子也能完成解析XML文件,但是如果XML文件很复杂,有很多层节点,初学者可能就会遇到麻烦。
这时候该怎么做呢?
可以参考下面的代码,自己看,我也是初学者。
关键在代码的16行,想想人家是怎么做的。
1 /** 2 * Parse top-level packets in order to process them further. 3 * 4 * @param thread the thread that is being used by the reader to parse incoming packets. 5 */ 6 private void parsePackets(Thread thread) { 7 try { 8 int eventType = parser.getEventType(); 9 do { 10 if (eventType == XmlPullParser.START_TAG) { 11 int parserDepth = parser.getDepth(); 12 ParsingExceptionCallback callback = connection.getParsingExceptionCallback(); 13 if (parser.getName().equals("message")) { 14 Packet packet; 15 try { 16 packet = PacketParserUtils.parseMessage(parser); 17 } catch (Exception e) { 18 String content = PacketParserUtils.parseContentDepth(parser, parserDepth); 19 UnparsablePacket message = new UnparsablePacket(content, e); 20 if (callback != null) { 21 callback.handleUnparsablePacket(message); 22 } 23 continue; 24 } 25 connection.processPacket(packet); 26 } 27 else if (parser.getName().equals("iq")) { 28 IQ iq; 29 try { 30 iq = PacketParserUtils.parseIQ(parser, connection); 31 } catch (Exception e) { 32 String content = PacketParserUtils.parseContentDepth(parser, parserDepth); 33 UnparsablePacket message = new UnparsablePacket(content, e); 34 if (callback != null) { 35 callback.handleUnparsablePacket(message); 36 } 37 continue; 38 } 39 connection.processPacket(iq); 40 } 41 else if (parser.getName().equals("presence")) { 42 Presence presence; 43 try { 44 presence = PacketParserUtils.parsePresence(parser); 45 } catch (Exception e) { 46 String content = PacketParserUtils.parseContentDepth(parser, parserDepth); 47 UnparsablePacket message = new UnparsablePacket(content, e); 48 if (callback != null) { 49 callback.handleUnparsablePacket(message); 50 } 51 continue; 52 } 53 connection.processPacket(presence); 54 } 55 // We found an opening stream. Record information about it, then notify 56 // the connectionID lock so that the packet reader startup can finish. 57 else if (parser.getName().equals("stream")) { 58 // Ensure the correct jabber:client namespace is being used. 59 if ("jabber:client".equals(parser.getNamespace(null))) { 60 // Get the connection id. 61 for (int i=0; i<parser.getAttributeCount(); i++) { 62 if (parser.getAttributeName(i).equals("id")) { 63 // Save the connectionID 64 connection.connectionID = parser.getAttributeValue(i); 65 } 66 else if (parser.getAttributeName(i).equals("from")) { 67 // Use the server name that the server says that it is. 68 connection.setServiceName(parser.getAttributeValue(i)); 69 } 70 } 71 } 72 } 73 else if (parser.getName().equals("error")) { 74 throw new StreamErrorException(PacketParserUtils.parseStreamError(parser)); 75 } 76 else if (parser.getName().equals("features")) { 77 parseFeatures(parser); 78 } 79 else if (parser.getName().equals("proceed")) { 80 // Secure the connection by negotiating TLS 81 connection.proceedTLSReceived(); 82 // Reset the state of the parser since a new stream element is going 83 // to be sent by the server 84 resetParser(); 85 } 86 else if (parser.getName().equals("failure")) { 87 String namespace = parser.getNamespace(null); 88 if ("urn:ietf:params:xml:ns:xmpp-tls".equals(namespace)) { 89 // TLS negotiation has failed. The server will close the connection 90 throw new Exception("TLS negotiation has failed"); 91 } 92 else if ("http://jabber.org/protocol/compress".equals(namespace)) { 93 // Stream compression has been denied. This is a recoverable 94 // situation. It is still possible to authenticate and 95 // use the connection but using an uncompressed connection 96 connection.streamCompressionNegotiationDone(); 97 } 98 else { 99 // SASL authentication has failed. The server may close the connection 100 // depending on the number of retries 101 final SASLFailure failure = PacketParserUtils.parseSASLFailure(parser); 102 connection.processPacket(failure); 103 connection.getSASLAuthentication().authenticationFailed(failure); 104 } 105 } 106 else if (parser.getName().equals("challenge")) { 107 // The server is challenging the SASL authentication made by the client 108 String challengeData = parser.nextText(); 109 connection.processPacket(new Challenge(challengeData)); 110 connection.getSASLAuthentication().challengeReceived(challengeData); 111 } 112 else if (parser.getName().equals("success")) { 113 connection.processPacket(new Success(parser.nextText())); 114 // We now need to bind a resource for the connection 115 // Open a new stream and wait for the response 116 connection.packetWriter.openStream(); 117 // Reset the state of the parser since a new stream element is going 118 // to be sent by the server 119 resetParser(); 120 // The SASL authentication with the server was successful. The next step 121 // will be to bind the resource 122 connection.getSASLAuthentication().authenticated(); 123 } 124 else if (parser.getName().equals("compressed")) { 125 // Server confirmed that it's possible to use stream compression. Start 126 // stream compression 127 connection.startStreamCompression(); 128 // Reset the state of the parser since a new stream element is going 129 // to be sent by the server 130 resetParser(); 131 } 132 } 133 else if (eventType == XmlPullParser.END_TAG) { 134 if (parser.getName().equals("stream")) { 135 // Disconnect the connection 136 connection.disconnect(); 137 } 138 } 139 eventType = parser.next(); 140 } while (!done && eventType != XmlPullParser.END_DOCUMENT && thread == readerThread); 141 } 142 catch (Exception e) { 143 // The exception can be ignored if the the connection is 'done' 144 // or if the it was caused because the socket got closed 145 if (!(done || connection.isSocketClosed())) { 146 synchronized(this) { 147 this.notify(); 148 } 149 // Close the connection and notify connection listeners of the 150 // error. 151 connection.notifyConnectionError(e); 152 } 153 } 154 }
只能帮你到这儿了!!!