How to search in JTree/xml - java

I am creating a JTree from an xml file.
Q: I have to implement a search functionality .
I have done it using JTree and I have observed that its too slow and my tree is quite heavy.
Q. Please suggest if I can implement using xml file

I always suggest UI programmers to avoid creating huge user interface widgets such as trees and lists.
The best solution is to parse your XML file, keep it in memory if it's size allows it, perform the search in the memory and then display ONLY the result of the search !!!
If the file can't fit into memory, then you should have a look on XML Sax parser and look forward to allow your end users to navigate in the data represented by your XML file.
Manu

I hope I understand correctly :)
If You need to query collections of XML data then use XQuery / XPath (wich especially designed for XML processing).
Best Regards, Gedevan

Related

Java XML Reading an XML file from top to bottom

I want to read an XML starting from the top to the bottom using Java. However, I don't want to use recursive functions because I want to be able to jump to a different element and start reading from that position.
I've tried using getParent() and indexOf() methods (All three libraries below have these methods) to do this, but it's gotten very messy, mainly because the methods don't distinguish between attributes and elements.
I'm sure there must be a simple way to do this, but after trying dom4j, jdom, and xom, I still have not found a solution.
[Edit] More Information:
My friend wants to make a console text-based game in a question/answer type style. Instead of hard-coding it into java, I decided to try and make it read an XML file instead, because XML has a tree-like style that would be convenient. Here is an example of what my XML file might look like:
<disp>Text to be displayed</disp>
<disp>Text to be displayed afterward</disp>
<disp>What is your favorite color?</disp>
<question>
<answer name="orange">
<disp>Good choice.</disp>
<!-- More questions and stuff -->
</answer>
<default>
<disp>Wrong. The correct answer was orange.</disp>
</default>
</question>
I don't know if it taboo to use XML like an pseudo programming language. If anyone has other suggestions feel free to give them.
Your design is basically good and an example of Declarative Programming. You should read your XML files using an XML parser either into a DOM or using SAX. Since I think you will want to revisit nodes I suspect you will need a DOM (FWIW I use XOM, xom.nu). One of the best examples of XML-based declarative programming is XSLT where the data and commands are all XML.
I use this model a great deal. It has the advantage that the data structure can be external and can be edited.
(Note that your XML needs a root element)
but it's gotten very messy, mainly because the methods don't
distinguish between attributes and elements.
All DOM or SAX tools differentiate very clearly between attributes and elements, so if there is confusion it is somewhere else.
It would be good if you would show what you want to do with the XML snippets, but normally if you want to read off of any kind of file, use java.util.Scanner.
Scanner scan = new Scanner (new File("file.xml"));
while (scan.hasNext()) {
String theData = scan.nextLine();
}
scan.close();
This should return the values that you need until it runs out of lines to scan.
Hope it works and Happy Coding!
Since you want to read a xml file. I recommend go for SAX parser. It is event based parser very fast and efficient for xml reading (top down approach).
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/ will explain about usage of sax parser.
Thanks

Sorting XML data by element?

If one needed to be able to display certain elements that contain some certain data and then sort them based on this data.
Which would be a better choice for a XML parser, DOM or SAX?
Also can either of these achieve sorting of XML data without the need of storing the data first?
Sorting will require you to read in all of the XML document to memory. So working with a DOM will probably be easier. There are good libraries available that make working with a DOM easier:
dom4j
JDOM
It would be a better to use STAX (Streaming API for XML), because it is universal solution for tiny or large files, but if your XML files isn't bigger you could use DOM, because it will be easier. Also you could make xpath query when using DOM, that could be helpful for you.
Woodstox
Aalto XML processor

Xml Query in java?

I am new to this validation process in Java...
-->XML file named Validation Limits
-->Structure of the XML
parameter /parameter
lowerLimit /lowerLimit
upperLimit /upperLimit
enable /enable
-->Depending the the enable status, 'true or false', i must perform the validation process for the respective parameter
--> what could be the best possible method to perform this operation...
I have parsed the xml (DOM) [forgot this to mention earlier] and stored the values in the arrays but is complicated with lot of referencing that take place from one array to another. If any better method that could replace array procedure will be helpful
Thank you in advance.
Try using a DOM or SAX parser, they will do the parsing for you. You can find some good, free tutorials in the internet.
The difference between DOM and SAX is as follows: DOM loads the XML into a tree structure which you can browse through (i.e. the whole XML is loaded), whereas SAX parses the document and triggers events (calls methods) in the process. Both have advantages and disadvantages, but personally, for reasonably sized XML files, I would use DOM.
So, in your case: use DOM to get a tree of your XML document, locate the attribute, see other elements depending on it.
Also, you can achieve this in pure XML, using XML Schema, although this might be too much for simple needs.

Reading data from XML file in Java

I dont know how to read data from such XML file. Lets say i want to read every every GUID and userID. How do i do it?
Here is part of XML: http://pastebin.com/7B25eyFz
if your xml file is Tree base then use DOM, if it is not nested then use SAX, is faster then DOM.
You may use Xstream
Look into SAX Parser. Also, do a search for your terms - there are a ton of questions about this topic.
Have you read the trail about XML of the Java tutorial?
You should use an XML library like XOM. You can then use it to query the XML document using XPATH. XOM offers a tutorial.
Adding to #user651407 point, If you just want to read the XML then go for SAX, It parses the XML in serial fashion so its faster, but if you want to do more complex operation like Adding, Updating or deleting a node then go for DOM but DOM Has Limitation
1. required more memory as entire XML is loaded at a time.
2. Slow in processing as it is a tree based parser.

Reading only root element in XML

In many REST based API calls, we have this parameter called nextURL, using which we can query for the next URL. This is usually in the root element.(or may be the next one)
In general how do you guys read this? In case you are using a standard XML parser, it reads and loads the entire XML and then you get to read the nextURL by getElementsByTag. Is there a better work around? Reading the entire xml is of course waste of time/memory.
Edit: An example XML would be something like
<result pubisher="xyz" nextURL="http://actualurl?since_date=<newdate>">
<element>adfsaf</element>
..
</result>
I need to capture the new since_date without reading the entire XML.
Python: You could use the ElementTree iterparse method ... provided the data you want is in an attribute, which will have been parsed by the time that you get the start event. If it's in the text or tail of the element, you will have to wait until the end event. It would be a good idea if you edited your question to show what your XML looks like, and explain "or maybe in the next one" with an example.
The term "Standard XML parser" covers a lot of territory, so much so that I don't think that you can generalize their behaviors. For instance, a standard DOM parser is tree-based and will read the entire XML into memory, but a SAX parser (and I think StAX as well) won't but rather will advance as the app desires it to advance. It sounds like the latter, a SAX or StAX parser, is what you need.
Edit: Please be sure to read KitsuneYMG's comment below on the difference between SAX and StAX behaviors.

Categories

Resources