1

So I've been given an XML-file of the format

<Level1 name="Foo1">
  <Level2 name="Foo2">
    <Level3 name="Foo3">
    </Level3>
  </Level2>
</Level1>

I'm supposed to read to parse a file formatted accordingly in my Java program. Does Java have any built-in functionality with XML (for example seeing what level a node is on etc) or do I have to scan it using a regular scanner and just read the input as I would a normal text file? I've been specifically told not to use any existing parser or external libs or such.

Nyfiken Gul
  • 654
  • 4
  • 20
  • 1
    How many seconds of research did you do? What I mean is: google finds you tons and tons of links for "java xml parsing" in 1 second; whereas it took you probably 1 minute to write that question? – GhostCat Apr 28 '17 at 11:48
  • 1
    `Does Java have any built-in functionality with XML (for example seeing what level a node is on etc)` - Well, yes, `SAXParser`. But you have `been specifically told not to use any existing parser`, so you are on your own with manual parsing (good luck with that) – BackSlash Apr 28 '17 at 11:49
  • 1
    My dear friend in case you do not have to use any other library , then it is most obvious that only things available to you are the java file readers/writers. So which means you need to use general java file parsing and build your program. – rootExplorr Apr 28 '17 at 11:51
  • 1
    @GhostCat Exactly. I think asking someone to not use existing parser is totally nonsense, but if he has been asked to do so his only option is to write a parser from scratch :/ – BackSlash Apr 28 '17 at 11:56
  • "I've been specifically told not to use any existing parser". In my view that means this is not a suitable question for StackOverflow. You can't expect the community to help you solve problems with such ridiculous constraints on the solution. – Michael Kay Apr 29 '17 at 22:43

1 Answers1

3

As the comments conclude: you can't have it both ways. If your assignment says to "parse XML manually" and you are not allowed to use libraries; then that is what you have to do.

In that sense: you turn to build-in classes like Scanner; and use its tokenizing capabilities to parse your XML; see here for some guiding ideas.

And from there: you can solve this with very varying amounts of effort. Like in: do you need to parse "real" XML; or will the incoming files have a fixed format?

You see, when you always get 6 lines exactly looking like your example; then you could get away with reading 6 lines of text; and using regexes, split, substring, ... operations to just fetch the "variable" contents.

But of course, when you are asked to parse anything that is valid XML then you need a true parser that emits events; and a "framework" to "do something" about the different kind of events. That other question contains an answer (no. 2 by 'ratchet freak') that outlines how to do that (obviously in another language).

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I see. I will not get a fix amount of nodes but the format will indeed be fixed, so I kind of know what to expect. I think the links you posted show the basic idea of what I need, thanks for your answer! :) – Nyfiken Gul Apr 28 '17 at 16:19
  • If you are going to parse some subset of XML with your own parser, then the first thing you should do is to define that subset of XML very precisely and formally. – Michael Kay Apr 30 '17 at 15:23