2011-10-07 125 views
0

下面給出的xml的任何人都可以提供有關如何分析這樣的XML並存儲到一個對象如何解析在黑莓

<DType>1</DType> 
<SId>abcdef</SId> 
<DT>20110922</Date> 
<Cause>F DOA</Cause> 
<Request> 
    <No>000047895</No> 

    <Name>mith</Name> 
</Request> 
<Token>AUD</Token> 
<Header> 

    <Item align="Centre" Title="Description">Request to purchase new shoes</Item> 
    <Item align="Left" Title="">FDOA</Item> 

    <Item align="Left" Title="Supplier">Chews</Item> 
    <Item align="Right" Title="Cost">$545</Item> 
</Header> 
<LItems> 

     <Column align="Left" Currency="N" Title="Qty">2</Column> 

     <Column align="Right" Currency="Y" Title="Price">1.25</Column> 
     <Column align="Right" Currency="Y" Title="Total">2.50</Column> 
    </LItem> 
    <LItem> 
     <Column align="Left" Currency="N" Title="Qty">10</Column> 
     <Column align="Right" Currency="Y" Title="Price">5.00</Column> 
     <Column align="Right" Currency="Y" Title="Total">500.00</Column> 

    </LItem> 
</LItems> 
<Footers>Approval of this request is subject company policy </Footers> 

回答

1

使用瞭解析kxml API的想法,請參閱this example

+0

我們如何能夠解析XML,如果它包含相同的標籤,但不同的屬性 –

+0

獲取元素列表,併爲每個元素檢查其屬性 –

1

試試這個XML解析

public class XMLDOMUtil { 
// go thru the list of childs and find the text associated by the tag 
public String getNodeTextByTag(Node parentNode, String name) { 

    Node node = parentNode.getFirstChild(); 
    Text text = null; 
    String retStr = null; 

    while (node != null) { 
     if (node.getNodeName().equals(name)) { 
      text = (Text) node.getFirstChild(); 
      retStr = text.getData(); 
      break; 
     } 
     node = node.getNextSibling(); 
    } 
    return retStr; 
} 

public Node getNodeByTag(Node parentNode, String name) { 

    Node node = parentNode.getFirstChild(); 
    Node retNode = null; 

    while (node != null) { 
     if (node.getNodeName().equals(name)) { 
      retNode = node; 
      break; 
     } 
     node = node.getNextSibling(); 
    } 
    return retNode; 
} 

public Node getNextSiblingNodeByTag(Node siblingNode, String name) { 

    Node retNode = null; 

    siblingNode = siblingNode.getNextSibling(); 

    while (siblingNode != null) { 
     if (siblingNode.getNodeName().equals(name)) { 
      retNode = siblingNode; 
      break; 
     } 
     siblingNode = siblingNode.getNextSibling(); 
    } 
    return retNode; 
} 

} 

然後在你的代碼,

DocumentBuilderFactory factory1 = DocumentBuilderFactory 
            .newInstance(); 
          DocumentBuilder builder1 = factory1 
            .newDocumentBuilder(); 
          XMLDOMUtil xm1 = new XMLDOMUtil(); 
          ByteArrayInputStream bis = new ByteArrayInputStream(
            responce.getBytes("UTF-8")); 
          Document document = builder1.parse(bis); 
          String DType= xm1.getNodeTextByTag(document _user, 
           "DType"); 
String SId= xm1.getNodeTextByTag(document _user, 
            "SId");... 

等等....

+0

我們可以使用此函數獲取Item標籤中的「標題」屬性。變量「document_user」的含義是什麼?它的價值是什麼?您可以使用標題 –

+0

。對不起 - 它不是document_user,它的用戶。 – Signare