| Author |
getting new line as first child
|
adithya kallu
Greenhorn
Joined: Nov 01, 2005
Posts: 23
|
|
Hello all, iam using DOM api to parse a simple xml file with structure like <filer> <name> </name> <age> </age> </filer> but when iam asking for first child of filer its returning me the new line character instead of "name: instead it returning "name" as its second child,same case with "age" after "name" appreciate your response
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
|
Hmm. I didn't see a question mark anywhere in your post. Did you mean to ask why you're getting a newline character as the first child? If so, then the answer is "because DOM considers text nodes being nodes just like element nodes".
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
adithya kallu
Greenhorn
Joined: Nov 01, 2005
Posts: 23
|
|
Firstly iam sorry for missing the "?" i meant to ask what i should do in order to avoid the newline characters being treated as child elements by the way i want to do this without using a dtd or schema Thank You
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
If you want to ignore them then by all means write some code that ignores them. Hint: the trim() method of String removes whitespace, so to tell if a text node is all whitespace:
|
 |
adithya kallu
Greenhorn
Joined: Nov 01, 2005
Posts: 23
|
|
Actually i like to find a way to configure the parser so that it automatically ignores whitespaces without using a dtd or schema Regards
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
|
|
In the Java 5 javax.xml.parsers.DocumentBuilderFactory we find the setIgnoringElementContentWhitespace() method. I have not actually tried this but it looks like it could do the job. Bill
|
Java Resources at www.wbrogden.com
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Originally posted by William Brogden: In the Java 5 javax.xml.parsers.DocumentBuilderFactory we find the setIgnoringElementContentWhitespace() method. I have not actually tried this but it looks like it could do the job. Bill
Indeed it would, and does, except for the implementation detail, as documented: "Note that only whitespace which is directly contained within element content that has an element only content model (see XML Rec 3.2.1) will be eliminated." That "element only content model" translates into a DTD or a schema. How else is the parser supposed to know when whitespace is "ignorable" and when it isn't?
|
 |
 |
|
|
subject: getting new line as first child
|
|
|