2011-10-07 103 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