如何在Blackberry中解析下面给出的xml

时间:2011-10-07 09:10:52

标签: xml parsing blackberry

任何人都可以了解如何解析这样的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>

3 个答案:

答案 0 :(得分:1)

使用kxml API进行解析,请参阅this example

答案 1 :(得分:1)

答案 2 :(得分: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");...

依旧......