wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Reading from XML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Reading from XML file" Watch "Reading from XML file" New topic
Author

Reading from XML file

Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Could somebody help me with this. I need to read from XML file but I need to parse it manual not with SAX,DOM etc. parsers.

This is only one example line from my XML file:



and this is part of my program:



But now I'm having problems as if I input some other line from my XML file, then program doesn't work properly. For example it doesn't output all attributes or it doesn't stop at > sign.

So I would like to know how to:

1. stop while when > sign occurs

or

2. how to write myLine.substring().indexOf(">) so that it can work properly with every line from my file

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Why can't you use SAX? I can imagine DOM is a problem if the XML document becomes large, but SAX does not have a huge memory footprint.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
I'm really beginner in this but my boss wants to make it this way so I must obey him.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Parsing XML this way is madness (it's a brittle solution, it's a maintenance nightmare, it takes time to develop and debug, etc. etc.). That's what the multitude of XML libraries and APIs are for.

Not only must you not obey this order, but you must explain to your boss why it is wrong.
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Yes, I know it's madness. I'm taking battle with this program for almost a month now.

But I must make it this way so if anyone knows solution to my problem, please help me.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Never process XML manually (i.e. with regexes, String operations...). It will go wrong and cost more than to do it right. Java (and any other mainstream language) has powerful tools for this.

Igor Juric wrote:my boss wants to make it this way so I must obey him.


You certainly have to follow his orders. But you also have a certain responsibility as software developer (or in any other profession) to note things that go terribly wrong. This is one of the occassions.

(More easily said than done, I know.)
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Igor Juric wrote:Yes, I know it's madness. I'm taking battle with this program for almost a month now.

But I must make it this way so if anyone knows solution to my problem, please help me.


Believe me... I tried this way once, and in no time i got lost. Have you discussed the matter with your boss? I mean using SAX in place of manually doing it with string manipulation?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
I think this discussion would sit better in another forum than "beginning". Moving thread.
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
I talked with my boss and told him that I found that SAX,DOM, Castor etc. parsers are for parsing XML file and I also made working program that way(by using DOM). But he insisted that I do it this way because, as he said, you at least know what you're doing. With those built-in parsers you don't know what is going on.

I think that main purpose for doing this way is that he wants to test my knowledge, he wants to test my basics as I told him that I programmed at least 4 years ago but still know some basics. And you're not only one you're telling me that I should use parsers that are built for that purposes.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I appreciate your situation.

as he said, you at least know what you're doing.

This sounds like "I know what you're doing" - implying that he has no clue about XML APIs.

With those built-in parsers you don't know what is going on.

The XML stuff built into the JRE wasn't done by some dingy fly-by-night outfit - it's used by many millions of people/applications every day. Assuming that they know what's going on, it seems that, in fact, it's just him who doesn't :-)

As to your code problem at hand, it would help if you posted one piece of input for which it does work, and one for which it doesn't.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Igor Juric wrote:But he insisted that I do it this way because, as he said, you at least know what you're doing. With those built-in parsers you don't know what is going on.


Knowing that you are doing it wrong is not helpful. Without completely understanding the XML specifications and knowing how to write a parser correctly you would not know what you need to do, you just would know what lines of code you wrote.

But that reasoning is often heard - not using a well-established framework and writing one by yourself because at a first glance the existing ones don't seem to support functionality that someone wants (like supporting rain in a game) or you want to fully control everything. Extremely expensive mistake.
Suhrid Karthik
Ranch Hand

Joined: Aug 31, 2008
Posts: 58

This reminds me of an epic answer to a similar question elsewhere on the web. Good for a few laughs
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Thanks for replies. I'll write two lines from file tomorrow as I'm not at work anymore and I don't have that XML file at home.

But I'll try to explain what makes me trouble. I want that my program extract attributes until first > sign, in example in first post, that is right after maticnaPps="value". So this part myLine.substring(0,15).indexOf(">) doesn't work properly. Well actually it does work on some lines from XML file but I want that it works on all lines.
Sometimes I need to increase to myLine.substring(0,20) or even more and sometimes I need to decrease it. If number is too big, then I got output without maticnaPps, if number is too small then it doesn't stop at > sign.

So basically all I don't know is how to write code so that it will stop at stop after first > sign. First I thought I can do it only by
while
but I couldn't figure out how. XML file has over 20k records and all records are pretty much same, it's just that some have more attributes in Tr, Imetnik, some less.

I can post my entire code if you need.

I really appreciate all your help, thanks!
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Igor Juric wrote:Well actually it does work on some lines from XML file but I want that it works on all lines.


Sorry for not giving you working code (again), but this requirement is exactly the reason why you must use a XML parser.

Or take some months off for learning the basics of parser generating and XML specification reading and writing one yourself. But not a "this is just the subset needed for this task using regex"-type. That plainly, as you now experience, does not work.

(Hints: Whitespace handling in XML ("Lines"??). Possible positions of the > character.)
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
I'm working on this for almost a month now and I'm little embarassed as I still don't have working problem but this is really tough nut for me.
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Ok, I'll post some examples and write down my output.

...(myLine.substring(0,20).indexOf(">") > 0)...

INPUT:


OUTPUT:


...(myLine.substring(0,20).indexOf(">")...

INPUT 1:


OUTPUT 1:



So as you can see, in first exampe it works properly. But in second, it seems that myLine.substring(0,20) is too small and then it doesn't stop at > sign after maticnaPps. If I increase second input to substring(0,25) then this is my output:



As you can see, maticnaPps is missing so it ends earlier.



Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371


Output:


Do you see now why no one is willing to debug manual XML parsing code?
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Yes, I know. But I can't argue with my boss, I'm working here for a month, this is my first task he gave me.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Are you sure he isn't just testing you on how professionally you're approaching tasks? Going along with this phenomenally bad design would indicate that the answer is "not very".
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
I think I made it. I tried this with random lines from XML file and it worked with all of them. Now I need to write this into mySQL table

Here is my code:

Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Lester Burnham wrote:Are you sure he isn't just testing you on how professionally you're approaching tasks? Going along with this phenomenally bad design would indicate that the answer is "not very".


I don't know if that is what he wants as I already did working program with SAX parser but he told me that I do it that way for reasons I posted before.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I venture to say that you're not going to have much fun on that job, because that line of reasoning rules out using any and all 3rd party libraries - no Apache Commons, no Struts/Spring/Hibernate, no using JDBC (because that relies on 3rd party drivers) - you'll have to write it all from scratch. I say run -don't walk- away from this.

Heck, SAX is even part of the JRE - if that's not allowed, what *is* allowed? <sarcasm>How can he allow you to use the String class? </sarcasm>
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
This rules out Java (as platform) for me ("You don't know when the GC will strike"). Or any operating system that wasn't written by himself. At least I would expect C or Assembler as language of choice when such claims are made.

---

Your code does not parse XML but only the sample given - which is one representation of infinite numbers of representations of this very same XML document. If your task is to parse this sample -or you can ensure that all documents follow the structurestyle of this sample to the last whitespace- then ok, you have a solution.

If your task is to write code that parses this XML, then you don't.
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
My task is that I parse lines from XML file. I now did that it opens XML file and reads its whole content.
First I did it with just one sample line because it's easier and you don't have to wait that program parse all 20k+ lines to see if it's doing right or wrong. AFAIK, it will be used only with this type of XML as I know it wouldn't work with others.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Igor Juric wrote:it will be used only with this type of XML


If you depend on "lines" and positions of tokens within these "lines" then you are not parsing XML but a file format with angled brackets that resembles XML. If you get these files from outside (customers) then you need a lot of luck and have to convince them not to produce XML but your file format.

If the files are produced within your organisational unit then it may work. It is still much to much work and will produce superfluous errors, but with some night shifts that typically can be worked around.

Better document NOW how your file format looks like and what assumptions you made. In a few weeks you will (naturally) have forgotten these. When the code breaks you need to know where to look. And you need to agree on these format with the file producer.
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Hi!

Sorry for late response. Yes, I totally agree with you. I doubt I will remember my code after few weeks/moths. But XML file won't change it's structure AFAIK. It if does, I'll do it then by SAX/StAX... parser.

One more thing. What would be now easiest way to insert data to mySQL table? I did it this way but it does nothing.

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
What do you think it should do? There's no code that does anything to the DB.

Which lines are or are not executed?
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
This doesn't do anything to DB?

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Nope. See the javadocs of updateString for how it works (and consequently, for what you need to do in your code).
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Thanks. I think I get it now. So I should use something like this, right?

But problem is if I try to use executeUpdate, I get error.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
That link goes to roseindia.net, which is rarely a source for good advice. Did you read the javadocs of updateString?
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
I did, if this is what you think?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
yes
Igor Juric
Ranch Hand

Joined: Nov 04, 2010
Posts: 31
Sorry but I don't understand what you want to tell me? Maybe that I can't update Tr column without insertRow() method ?

edit:

I tried now with updateRow() and it works but only one attribute from Tr updates? I can view only first rn value.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I tried now with updateRow() and it works but only one attribute from Tr updates? I can view only first rn value.

I have no idea what that means, but since this has nothing to do with XML, you should start a new topic in the JDBC forum to ask about JDBC questions.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Igor Juric wrote:But XML file won't change it's structure AFAIK.


I am not talking about the structure, which (obviously) triggers the need for refactoring the consumer.

I am talking about formal things, interpunctation, linebreaks, whitespaces, which, thanks to the XML specification, are allowed and do not need any work at the consumer side. If the consumer understands XML.
Spencer Fingleton
Greenhorn

Joined: Mar 30, 2010
Posts: 9
Igor...

Cant believe the attitude of your boss... it would take 5 mins to do this in say JAXB. I suggest you find a new job

Raoul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from XML file
 
Similar Threads
trying to use a txt file like a database
Shift operators !
Shift Operator
Display output table
Foreign key problem