Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DOMSource?

 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone show me how to make the following codes to print out only once?

=========
For example, the code above prints

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SevenDwarfs>
<Row>
<name>Dopey</name>
<status>youngest</status>
</Row>
</SevenDwarfs>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SevenDwarfs>
<Row>
<name>Dopey</name>
<status>youngest</status>
</Row>
<Row>
<name>Doc</name>
<status>oldest</status>
</Row>
</SevenDwarfs>

==========
I would like to have only the latter portion printed as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SevenDwarfs>
<Row>
<name>Dopey</name>
<status>youngest</status>
</Row>
<Row>
<name>Doc</name>
<status>oldest</status>
</Row>
</SevenDwarfs>
=========
Also, how can I get rid of "<?xml version="1.0" encoding="UTF-8" standalone="no"?>" and just have the rootNode(<SevenDwarfs>) and the children nodes? I think it might have something to do with the way DOMSource is setup but I could be wrong. TIA.
 
Paul Clapham
Sheriff
Pie
Posts: 20757
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be a lot easier to read your code if it had been posted inside the "Code" tags (see the "Code" button above the box you post in?) and somebody might even read the whole thing if you edited your post to do that. Unindented code is very hard to read.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main method creates two XML trees (lines 79, 83)--what would you expect?
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reading, but if I take out line 79, it prints as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SevenDwarfs>
<Row>
<name>Doc</name>
<status>oldest</status>
</Row>
</SevenDwarfs>

That is not what I want.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't dig through the convoluted code enough.

Your createXMLRecords method is doing too many things: it both creates the XML records *and* does the transformation. You should only do the transformation once, at the very end.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(The root node creation code also does too much. A method should do only one thing, do it well, and do it completely... but no more.)
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I extracted line 66-70 to made another method which was called after line 83, but still I got the following printout:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SevenDwarfs>
<Row>
<name>Doc</name>
<status>oldest</status>
</Row>
</SevenDwarfs>
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the current code (UseCodeTags).
 
thomas silver
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this:(Once I add transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");, depending on your transformer implementation.) What's the difference between that and what you want?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic