• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean to say that i don't need the shouldFilter()
 
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make the changes as I outlined, then you wouldn't need the method because you can use the boolean variable.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i am not getting how to use the boolean variable
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to add all XML elements that are inside of the Cube element, correct? So you need to know when the parser is encountering elements that are inside the Cube element - that's what the variable accomplishes.

Are you completely clear on how a SAX parser goes about parsing an XML document, and how it reports results to your code?
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no i am not completely clear about sax parser
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean insideCube=false;

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(qName, atts));

// Add attributes as child nodes //

if (qName.equals("Cube")) {
insideCube=true;
}
if(insideCube){
attachAttributeList(currentNode, atts);
previousNode.add(currentNode);
super.startElement(uri, lName, qName, atts);

}
}

but i am not able to add them as child element of cube , i am able to add them as a child element of rootnode
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super.startElement(uri, lName, qName, atts);
ignore this line
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to think about which node you're adding new nodes to - only Cube nodes should be added to the root node. Nodes inside of Cube nodes should be added to the current Cube node.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so do i need to make changes in endElement() or startElement()
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably (but I'm not entirely on how you're keeping track of nodes).
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void endElement(String uri, String lName, String qName) throws SAXException {


if(currentNode==null){

 currentNode = (DefaultMutableTreeNode) currentNode.getParent();

}/*else if (qName.equals("Cube")) {
insideCube=false;

}*/else
startDocument();
}

this is my endElement
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Probably (but I'm not entirely on how you're keeping track of nodes).



Probably??
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, I'm not entirely clear how the code handles nodes. Have you tried anything yet?
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you replace

getNodeText(lName, atts)

with

getNodeText(qName, atts)

the results look pretty good.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is adding them to the rootnode
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I get. Are you not getting something very similar?
Screen-Shot-2018-08-29-at-16.18.28.png
[Thumbnail for Screen-Shot-2018-08-29-at-16.18.28.png]
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no
i am getting the aggregate table under lastscf.xml not under cube
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here's what i am getting
Capture.PNG
[Thumbnail for Capture.PNG]
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you share the required changes i need to make
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here's my code. I'm not sure what's different, but you're pretty close.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(currentNode!=null)
i was making mistake here

but now i am facing different problem
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now consider this xml file



now when i am running the code the following output i am getting
Capture1.PNG
[Thumbnail for Capture1.PNG]
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there's an exception, so what do you expect?

NullPointerException in line 95, so probably previousNode is null.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what to do now
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Examine why the exception happens, and then change the code to address the problem.

Seriously, you need to apply yourself harder to this problem. It seems you're lost every time you hit a snag, and just give up trying to work out what the problem is. That is not the way to success in software development.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved

thanks a lot
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i am new to programming

can you suggest me a way to improve my concepts
 
Tim Moores
Saloon Keeper
Posts: 5576
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing lots of it helps.

Breaking problems into pieces also helps: here you mixed XML processing with creating a Swing GUI - topics that have nothing to do with each other. Next time around, get the XML processing right before working on the GUI to display the results. That makes it easier to solve issues with the individual parts.
 
Krishnaaa singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay buddy

Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!