This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

XML to JTree Using SAX, how to add only required tag from the xml file to the tree  RSS feed

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi team,
i need to add only the specific tag to my tree


This code add all tags of xml file to the tree
i want only specific tags from the xml file ie i only want to add cube tag and its child element to the root node

here's my xml file






here's my java code

 
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameters of the startElement method have all the information you need for this. The node should be added only if the node name is "Cube". You basically already have all the code you need; it just needs to be organized a bit differently.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help me out with the code
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you got so far, I think you can do this on your own. A little hint: The line

if ("Cube".equals(lName) && atts.getValue("name") != null)

needs to find its way into the startElement method.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i Am not able to understand what i need to do
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently, the startElement method creates a node for each XML element. But you only want that for Cube elements. So you need to check the name of the element (which gets passed into the startElement method as a parameter), and only create a node if it matches "Cube".
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shouldFilter() method which i created is for that purpose only but when i am using it then no nodes are added to the tree
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*  if (shouldFilter(currentNode)) {*/

when i remove the comment and run the program then no nodes are added to the tree
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shouldFilter should not look at the name of the DefaultMutableTreeNode, it should look at the name of the XML element - which is passed into startElement as a parameter.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i tried this but still same result no nodes are being added to the tree
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void startElement(String uri, String qName, String lName,
Attributes atts) throws SAXException {



previousNode = currentNode;

// Use the result of the getNodeText method to construct the tree

currentNode = new DefaultMutableTreeNode(getNodeText(lName, atts));

// Add attributes as child nodes //
if (shouldFilter(lName)) {

attachAttributeList(currentNode, atts);

previousNode.add(currentNode);

}
}
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got the parameter list of startElement wrong - localName is the 3rd parameter, whereas qName is the second. Make sure "System.out.println(name)" prints out the actual element names.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry but i am not able to understand what changes i need to make
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of

public void startElement(String uri, String qName, String lName,
Attributes atts)

you should have

public void startElement(String uri, String lName, String qName,
Attributes atts)
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void startElement(String uri, String lName, String qName,
Attributes atts) throws SAXException {



previousNode = currentNode;

// Use the result of the getNodeText method to construct the tree

currentNode = new DefaultMutableTreeNode(getNodeText(lName, atts));

// Add attributes as child nodes //
if (shouldFilter(lName)) {

attachAttributeList(currentNode, atts);

previousNode.add(currentNode);

}
}

i did but stiil same result
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it working for you??
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just trying to help you (in this case by reading the javadocs), I'm not actually running the code. That's your task :-)
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay

what else can i do..
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Make sure "System.out.println(name)" prints out the actual element names


You should be looking for "Cube".
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the code encounters a true condition it stops it doesn't looks for other element
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it stop? There's no code to exit in the middle of an XML file.  
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

Tim Moores wrote:Make sure "System.out.println(name)" prints out the actual element names


You should be looking for "Cube".



it's printing out the actual names only
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Why does it stop? There's no code to exit in the middle of an XML file.  



i don't know why it's stopping when it is encountering Cube
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

Tim Moores wrote:Make sure "System.out.println(name)" prints out the actual element names


You should be looking for "Cube".



this is what this line is printing and stopping when encountering "Cube"

Application
ApplicationProperties
Notification
UIConfig
UserProfile
Sidebar
Group
Link
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you changed the code yet so that the shouldFilter method takes as parameter the localName?
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




this is my code after making changes still same reult
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There'S a NullPointerException that doesn't get displayed because the catch block ignores all exceptions.

Also, I was getting confused about lName and qName myself - it's the qName you need to use. Sorry about that.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (shouldFilter(qName)) {

attachAttributeList(currentNode, atts);

previousNode.add(currentNode);


}}

Still same output
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you fixed the nullpointerexception?
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to fix it?
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It happens in the line

currentNode = (DefaultMutableTreeNode) currentNode.getParent();

Presumably, currentNode is null. What should happen in that case?
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (currentNode.getUserObject().equals(lName)){
 currentNode = (DefaultMutableTreeNode) currentNode.getParent();

}

i tried this but still nullpointerexception
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:It happens in the line

currentNode = (DefaultMutableTreeNode) currentNode.getParent();

Presumably, currentNode is null. What should happen in that case?



if currentNode is null then root element should be assigned to it
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am able to attach cube element to the tree but not cube child element under cube

here is my code

public void startElement(String uri, String lName, String qName,
Attributes atts) throws SAXException {


/*System.out.println(uri);
System.out.println(qName+"k");
System.out.println(lName+"l");*/
previousNode = currentNode;

// Use the result of the getNodeText method to construct the tree
/* System.out.println(currentNode+"g");*/
currentNode = new DefaultMutableTreeNode(getNodeText(lName, atts));

// Add attributes as child nodes //

if (shouldFilter(lName)) {

attachAttributeList(currentNode, atts);

previousNode.add(currentNode);


}}


public void endElement(String uri, String lName, String qName) {
System.out.println(currentNode);
if(currentNode!=null){

 currentNode = (DefaultMutableTreeNode) currentNode.getParent();

}else
{
currentNode=rootNode;
}

}
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am able to attach cube element to the tree but not cube child element under cube


Well, of course. The shouldFilter method only returns true for "Cube", so all other elements will be filtered out.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what should i do now
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check whether any elements you encounter are inside of a Cube element, and treat them differently. Something like this:



Then the shouldFilter method simply becomes "insideCube".
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i didn't get it
 
Tim Moores
Saloon Keeper
Posts: 5038
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specifically? Looking at the code, what do you think it accomplishes? Why might it be useful, given what you need to change in your code?
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  if (qName.equals("Cube"))
       insideCube = true;

 i m not getting what this line of code is doing
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!