• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why no main?  RSS feed

 
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to learn enough so I can parse an XML document and count how many times a particular node appears.
Since I'm a beginner in programming, I can't understand why one would call the below a complete code. There is no public static void main!

So the bug (programmer joke) question: Why no main??
 
Marshal
Posts: 56603
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:. . . So the bug . . .
There is no bug in it. Where did you get that code from? You realise it is very old code, and nobody would write code like that now, with Vector and Hashtable?

That is a class which parses an XML document and it appears to be a utility class. You can tell that from the private constructor and the many static methods. You would obviously have to write a class with a main method which calls its methods. It is difficult to know how to use that class because th documentation comments are poor. It would be something like reading the file into a single String and passing it to the readXML method.
List<String> list = XmlReader.readXML(myXmlText);

I shall duplicate this discussion in our XML forum, and maybe somebody will give you something more modern to use.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Sergiu Dobozi wrote:. . . So the bug . . .

I shall duplicate this discussion in our XML forum, and maybe somebody will give you something more modern to use.


There was no bug, I just replaced big with bug as a joke. This code or the one below were the ones I wanted to use, they were written three years old. Got them from http://beginnersbook.com/2013/03/parsing-xml-using-dom4j-java-api/
Is this piece of code outdated as well?
 
Sheriff
Posts: 4932
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:Is this piece of code outdated as well?
It isn't, but it doesn't do much. It does return root node name and that is it.

Probably before you use any parser, you need to consider if the xml file/-s you're about to parse is/are relatively big or small.

For a small ones, probably you can use DOM as it should work faster, because all parsed nodes as a tree being loaded to memory and then parsed. The problem with such approach arise when XML is huge, then you can easily run out of memory. In order to avoid that, you'd need to look into SAX parser (as one of the options).

Community members hardly can help you if you don't tell them the whole picture of the project and later specific details you need to follow. Currently we're randomly shooting in providing you some correct/incorrect answers.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Sergiu Dobozi wrote:Is this piece of code outdated as well?
It doesn't, but it doesn't do much. It does return root node name and that is it.

Probably before you use any parser, you need to consider if the xml file/-s you're about to parse is/are relatively big or small.

For a small ones, probably you can use DOM as it should work faster, because all parsed nodes as a tree loading being loaded to memory and then parsing. The problem with such approach arise when XML is huge, then you can easily run out of memory. In order to avoid that, you'd need to look into SAX parser (as one of the options).

Community members hardly can help you if you don't tell them the whole picture of the project and later specific details you need to follow. Currently we're randomly shooting in providing you some correct/incorrect answers.


I'm really new to programming in general, but I managed to understand Java enough to create some simple math programs with a little OOP. A company I spoke to said my CV would be welcome if I also made a small project (parsing an XML file and counting how many times the node "Ship" appears was an example).
This website was the first resource I was drawn to, in my quest to read more about parsing XML files. Now I realize that the post is 3 years old. So that's the thing, there is no specific project in mind, I think it would actually be simple to make, if I had some basic understanding about this area of programming.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:Since I'm a beginner in programming, I can't understand why one would call the below a complete code. There is no public static void main!

Most Java classes don't actually have a public static void main(String[] args) method. Since that's a special method that Java uses as an entry point into any program/application, you only need one. Applications can be made up of hundreds, if not thousands of classes and the vast majority of those are going to be sans a main() method.  In fact, in any Java application that has any N number of classes, you should expect to have exactly ONE main() method that is used as the application's primary entry point.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Sergiu Dobozi wrote:Since I'm a beginner in programming, I can't understand why one would call the below a complete code. There is no public static void main!

Most Java classes don't actually have a public static void main(String[] args) method. Since that's a special method that Java uses as an entry point into any program/application, you only need one. Applications can be made up of hundreds, if not thousands of classes and the vast majority of those are going to be sans a main() method.  In fact, in any Java application that has any N number of classes, you should expect to have exactly ONE main() method that is used as the application's primary entry point.

I knew that, I was just confused why he left out the main method here, since I reckoned there was only one class in this application; Now I realize he left it out because the main method was pretty straightforward to write for non-beginners.
 
Liutauras Vilda
Sheriff
Posts: 4932
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

No. Junilu actually mentioned that already. Most of the classes don't have main method, as the one you showed us.
That class implements (assume correctly) XML parsing functionality and is ready to use in the bigger picture, as a part of main/actual application, so it doesn't need to have main method, in fact, it shouldn't.

 
Liutauras Vilda
Sheriff
Posts: 4932
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that other application may look like:
 
Campbell Ritchie
Marshal
Posts: 56603
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:. . .  there was only one class in this application; . . . .
No, as Liutauras and Junilu have explained, that class is not an application. It is intended to be part of an application. So it doesn't need a maint method.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone. I feel that I understand now.
 
Campbell Ritchie
Marshal
Posts: 56603
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's a pleasure
And ... to the resolved button!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!