aspose file tools
The moose likes XML and Related Technologies and the fly likes getting new line as first child Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "getting new line as first child" Watch "getting new line as first child" New topic
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
    
    5
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
    
    2

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
    
    1
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
    
    2

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?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: getting new line as first child
 
Similar Threads
Dont know how to map if there is no primary key???
jsp:getProperty
how to remove particular child element
javaranch mock question
Recursively parse XML and splitting into separate XMLs