• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting new line as first child

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
adithya kallu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i like to find a way to configure the parser so that it automatically ignores whitespaces without using a dtd or schema

Regards
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'm so happy! And I wish to make this tiny ad happy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic