若要剖析 CRNP 伺服器應註冊或取消註冊訊息而傳送的 SC_REPLY XML 訊息,您需要 RegReply 輔助程式類別。您可以從 XML 文件建構此類別。此類別為狀況碼與狀況訊息提供 accessor。若要剖析伺服器的 XML 串流,您需要建立新的 XML 文件,並使用該文件的剖析方法。http://java.sun.com/xml/jaxp/index.html 中的 JAXP 文件將更詳細地說明此方法。
建立實施前導邏輯的 Java 程式碼。
請注意,readRegistrationReply 方法使用新的 RegReply 類別。
private void readRegistrationReply(InputStream stream) throws Exception
{
// Create the document builder
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new DefaultHandler());
//parse the input file
Document doc = db.parse(stream);
RegReply reply = new RegReply(doc);
reply.print(System.out);
}
實施 RegReply 類別。
請注意,retrieveValues 方法在 XML 文件的 DOM 樹狀結構中行走,然後取出狀況碼與狀況訊息。http://java.sun.com/xml/jaxp/index.html 中的 JAXP 文件包含更多詳細資訊。
class RegReply
{
public RegReply(Document doc)
{
retrieveValues(doc);
}
public String getStatusCode()
{
return (statusCode);
}
public String getStatusMsg()
{
return (statusMsg);
}
public void print(PrintStream out)
{
out.println(statusCode + ": " +
(statusMsg != null ? statusMsg : ""));
}
private void retrieveValues(Document doc)
{
Node n;
NodeList nl;
String nodeName;
// Find the SC_REPLY element.
nl = doc.getElementsByTagName("SC_REPLY");
if (nl.getLength() != 1) {
System.out.println("Error in parsing: can't find "
+ "SC_REPLY node.");
return;
}
n = nl.item(0);
// Retrieve the value of the statusCode attribute
statusCode = ((Element)n).getAttribute("STATUS_CODE");
// Find the SC_STATUS_MSG element
nl = ((Element)n).getElementsByTagName("SC_STATUS_MSG");
if (nl.getLength() != 1) {
System.out.println("Error in parsing: can't find "
+ "SC_STATUS_MSG node.");
return;
}
// Get the TEXT section, if there is one.
n = nl.item(0).getFirstChild();
if (n == null || n.getNodeType() != Node.TEXT_NODE) {
// Not an error if there isn't one, so we just silently return.
return;
}
// Retrieve the value
statusMsg = n.getNodeValue();
}
private String statusCode;
private String statusMsg;
}