Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Parse XML to String ?

I would personally change the way you have the xml structured to something like this:

 

<Cars>
 <Car>
  <Name>
  <Type>
  <Size>
  <Whatever>
 </Car>
</Cars>

 

 

and Parsing is easy.

 

NodeList list = node.getNodeList();

loop the list and parse the XML.

Contributor
Posts: 27
Registered: ‎05-28-2010
My Device: 9000
My Carrier: Airtel

Re: Parse XML to String ?

Then you need  to iterate through all the Nodes by getting


NodeList listChildNodes = node.getChildNodes();


from the given Element. And then traverse through all the Nodes one by one using

int length = listChildNodes.getLength();

and loop on length and get the resultant values.


for(int index = 0;index < length ;index++){

          

                Node childNode = listChildNodes.item(index);


        if (childNode != null && childNode.getNodeType() == Node.TEXT_NODE) {



            // if the text content contains the text then
            String nodeValue = childNode.getNodeValue();                   
            //Or get the whatever attributes value you want.
         }
}

Developer
Posts: 206
Registered: ‎07-02-2010
My Device: 8320 Curve
My Carrier: Vodacom

Re: Parse XML to String ?

Guys i know you are trying to help and i thank you. But you are not understanding me. I don't want to know how to manipulate the xml. I know how to remove childnode and add attributes etc. I just want the manipulated xml's source in String format.

 

lamens terms :

 

String xmlString = xml.Source

 

xmlString should return the xml source.

 

I am so bad at explaining sorry guys.

Developer
Posts: 206
Registered: ‎07-02-2010
My Device: 8320 Curve
My Carrier: Vodacom

Re: Parse XML to String ?

 

public String DeleteCar(String RegNo)
    {
        int depth = 0;
        String carDeleted = "";
        try
        {                       
            Node node = GetElelement();
            
            if ( node.getNodeType() == Node.ELEMENT_NODE)
            {    
                StringBuffer buffer = new StringBuffer();
                indentStringBuffer( buffer, depth );
                NodeList childNodes = node.getChildNodes();
                int numChildren = childNodes.getLength();
                Node firstChild = childNodes.item( 0 );
                
                if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE )
                {
                    buffer.append( node.getNodeName() ).append( " = \"" ).append( firstChild.getNodeValue() ).append( '"' );
                    NodeText = firstChild.getNodeValue().toString();
                }
                else
                {
                    buffer.append( node.getNodeName() );
                    Element el = null;
                    for ( int i = 0; i < numChildren; ++i )
                    {
                        boolean hasDeleted = false;
                        if(childNodes.item(i).getNodeName().toString().equals("car"))
                        {
                            int nodeAttributes =        childNodes.item(i).getAttributes().getLength();
                            for(int z = 0; z < nodeAttributes; z++)
                            {          
                                    if(RegNo.equals(childNodes.item(i).getAttributes().getNamedItem("registra                                tionnumber").toString()))
                                {
                                    node.removeChild(childNodes.item(i));
                                    childNodes = node.getChildNodes();
                                    numChildren = childNodes.getLength();
                                    hasDeleted= true;
                                    el = (Element)node;    
                                    
                                }
                                if(hasDeleted == true)
                                {
                                    break;
                                }
                            }                            
                        }
                    }
                    String xmlSource = ?????;
                    // Here i want to get the new XML Source
} } } catch (Exception e) { System.out.print(e.toString()); } return carDeleted; }

 

 

Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Parse XML to String ?

Do you mean that you want to parse the Document into a XML String?

Developer
Posts: 206
Registered: ‎07-02-2010
My Device: 8320 Curve
My Carrier: Vodacom

Re: Parse XML to String ?

If Parsing the Document returns a String that can be used to to Parse back to an xml Document then yes that is what i am trying to do.

Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Parse XML to String ?

In that case you need to create your own Document to XML parser.

It's easy to implement. All you need to do is loop the elements and manually add new lines, tabs, attributes, and the < > XML tags.

 

If anyone else knows how to achieve this using BlackBerry API's please post the code.

New Developer
Posts: 4
Registered: ‎07-23-2010
My Device: Not Specified

Re: Parse XML to String ?

[ Edited ]

I dont think you can get the xml string back from the "Document" or "Node" etc.. (I refered the BB document for that)

 

You can only create an custom xml string according to the values you remove/add to the node.

 

Hard-coding is one of the method you can achieve that, but I use thirdparty XML generator "NanoXML". It is free to use and modify..

Developer
Posts: 206
Registered: ‎07-02-2010
My Device: 8320 Curve
My Carrier: Vodacom

Re: Parse XML to String ?

Hello.

 

So just to understand correctly, I will not be able to get a String representation of an XML's Source from a Document, Element or Node ? The there is no way to have an XML in memory and save it's Source to a String once you have manipulated it ?

 

Then i am stumped !

 

:smileysad:

Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Parse XML to String ?

yes you can. Just create your own parser to parse the document to a string.