This discussion is archived
4 Replies Latest reply: Sep 29, 2011 3:59 PM by DrClap RSS

Is this possible using XML..?

891096 Newbie
Currently Being Moderated
Dear friends,

I am new to XML, but I have given a task, in which I have to use Java Swing along with XML for the project.
XML will be used to store data and front-end will be Java Swing.

This is a sample xml file which will store data -

<heading>
<Name>ABC</Name>
<Age>21</Age>
<City>NY</City>
<info>....</info>
</heading>
<heading>
<Name>PRS</Name>
<Age>21</Age>
<City>CA</City>
<info>....</info>
</heading>
<heading>
<Name>XYZ</Name>
<Age>22</Age>
<City>NY</City>
<info>....</info>
</heading>

There are thousands of such records. The <info>..</info> tag will contain some text in 3-4 paragraphs.

Is it possible with XML...
1. To find out how many records are of 21 age..?
2. To search the <info>...</info> with specific words..?
3. To count the records with city as NY..?
4. In a text box, if 21 is entered then a combobox would get populated with all records Name having age 21..?


:-( This might be a foolish way to ask like this... but I havent used XML, till now my projects where using DBs...

Please guide me...


Thanks in advance...
Have a nice day
John
  • 1. Re: Is this possible using XML..?
    800151 Newbie
    Currently Being Moderated
    Yes sure you can use a SAX parser for example.

    If your XML document is very big you should use a parser that does not read the entire XML document into the memory.

    See here chapter two:
    http://en.wikipedia.org/wiki/Java_API_for_XML_Processing

    => The SAX way it the way to do it :)

    There are dozens of examples on that in the web.

    Sebastian
  • 2. Re: Is this possible using XML..?
    796440 Guru
    Currently Being Moderated
    XML is just a markup language. There's nothing in the spec that says anything about querying. Now, just like any data, if you can read the data, you can of course write code that examines that data and stores information about it, such as the things you're looking for. But there's nothing about XML itself to provide support for that.

    If you're looking for a way to say "give me all the <heading> nodes that have an <age>21</age> child element", then you probably want XPath. Start here: http://www.w3schools.com/xpath/ or google for xpath tutorial for more information. Once you're familiar with the concepts of XPath in general, you can google for something like java xpath examples to see how to use it in your Java code.

    In all cases, stay away from any links that take you to roseindia.net. That site is so full of misinformation as to be worse than worthless.
  • 3. Re: Is this possible using XML..?
    796440 Guru
    Currently Being Moderated
    seba.wagner wrote:
    => The SAX way it the way to do it :)
    No. For what the OP wants, XPath is more appropriate.
  • 4. Re: Is this possible using XML..?
    DrClap Expert
    Currently Being Moderated
    seba.wagner wrote:
    If your XML document is very big you should use a parser that does not read the entire XML document into the memory.
    But we have been told that it's "thousands" of records. That is not very big at all. So the horrible nitpicky code which would have to be written to implement XPath over SAX is unnecessary.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points