07-30-2010 08:09 AM
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.
07-30-2010 08:11 AM
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.
}
}
07-30-2010 08:24 AM
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.
07-30-2010 08:28 AM
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().equ als("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;
}
07-30-2010 08:37 AM
Do you mean that you want to parse the Document into a XML String?
07-30-2010 08:46 AM
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.
07-30-2010 08:55 AM
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.
07-30-2010 09:20 AM - edited 07-30-2010 09:21 AM
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..
07-30-2010 09:39 AM
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 !
![]()
07-30-2010 09:42 AM
yes you can. Just create your own parser to parse the document to a string.