Pramod J George
Pramod J George

Reputation: 1723

how to parse the xml given below in Blackberry

Can anybody give an idea about how to parse such an xml and store it into an object

<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>

Upvotes: 0

Views: 234

Answers (3)

Rince Thomas
Rince Thomas

Reputation: 4158

Try this xml parsing

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;
}

}

then on your code,

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");...

and so on....

Upvotes: 1

paulkayuk
paulkayuk

Reputation: 1052

There is a good example using the SAXParser here: http://www.java-tips.org/java-me-tips/midp/introducing-xml-parsing-in-j2me-devices.html

Upvotes: 1

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

use kxml API for parsing, see this example

Upvotes: 1

Related Questions