BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-30-2005, 06:16 PM   #1 (permalink)
Knows Where the Search Button Is
 
timinator's Avatar
 
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default XML Parsing

Please Login to Remove!

Well, due to the lack of response on my FTP issue, I'm now importing a XML file via email attachement, which works fine btw. However, I seem to be running into an issue with the XML parsing routines. My XML file is structured like this:
<?xml version="1.0" ?>
- <Category Name="Master">
- <SubCat Name="Sub">
<Item1>My Item</Item1>
<Item2>My Item2</Item2>
</SubCat Name>
</Category>

Here's my problem. I can parse out the Items just fine by using getNodeName() and getNodeValue(). But what do I use to get the values of Category Name and SubCat Name? Only docs that I've come across is the RIM source example which loops through the nodes and values between tags, but it doesn't address what I'm trying to do here.
__________________
===================================
"There are 10 types of people in the world.
Those that understand binary and those that don't!"
www.timothytrimble.info - The ART of S/W Development
==================================
Offline  
Old 09-30-2005, 09:49 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Sep 2004
Model: 8700
Carrier: T-mo
Posts: 162
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Those are attributes that are associated with the tag.
Offline  
Old 10-01-2005, 01:42 PM   #3 (permalink)
Knows Where the Search Button Is
 
timinator's Avatar
 
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

An example on how to use getAttribute would be nice. Or at least point me to an example or doc that describes how to use it.
I've tried using getAttribute, as seen below, but it raises a "java.lang.ClassCastException" exception.

Code:
ByteArrayInputStream stream = new ByteArrayInputStream(data.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);      
factory.setAllowUndefinedNamespaces(true);              
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(stream);
Element root = doc.getDocumentElement();
Element operation = (Element) root.getOwnerDocument();
String _layer = operation.getAttribute("Category");
System.out.println("Category: " + _layer);
NodeList list = doc.getElementsByTagName("*");
_node = new String();
_element = new String();
for (int i=0;i<list.getLength();i++) {
       Node value = list.item(i).getChildNodes().item(0);
       _node = list.item(i).getNodeName();
       _element = value.getNodeValue();
      System.out.println("Node:Element = " + _node + ":" + _element);
}
__________________
===================================
"There are 10 types of people in the world.
Those that understand binary and those that don't!"
www.timothytrimble.info - The ART of S/W Development
==================================
Offline  
Old 10-02-2005, 09:56 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Sep 2004
Model: 8700
Carrier: T-mo
Posts: 162
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should be able to find in many JAXP DOM XML parsing example as it's a standard java API. Do a google search..

I suspect your line:
Element operation = (Element) root.getOwnerDocument();
is throwing the exception, not the getAttribute line.

- P
Offline  
Old 10-04-2005, 06:38 PM   #5 (permalink)
Knows Where the Search Button Is
 
timinator's Avatar
 
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've looked through a bunch of DOM discussions on the net but did not find anything that directly addressed what I'm trying to do. I'm thinking that the execption was happening because the first tag has <Category Name= (Two words with a space.) If I use a tag with only one word then it works fine.
__________________
===================================
"There are 10 types of people in the world.
Those that understand binary and those that don't!"
www.timothytrimble.info - The ART of S/W Development
==================================
Offline  
Old 10-05-2005, 06:18 AM   #6 (permalink)
abx
New Member
 
Join Date: Aug 2005
Model: 7290
Carrier: VF
Posts: 6
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
<SubCat Name="Sub">
.....
</SubCat Name>
I think this is no correct XML. Try </SubCat>
Offline  
Old 10-05-2005, 02:00 PM   #7 (permalink)
Knows Where the Search Button Is
 
timinator's Avatar
 
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unfortunately, the client has required the use of a tag with spaces in it. Tis okay. I wrote a custom parser that works just fine. Just wish I'd known about the DOM issue with spaces ahead of time. Tis part of the joy of programming - discovery! ;)
__________________
===================================
"There are 10 types of people in the world.
Those that understand binary and those that don't!"
www.timothytrimble.info - The ART of S/W Development
==================================
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.