TinyXml save-formatting when using CDATA blocks - tinyxml

If I have this test code:
TiXmlElement *parentElem = new TiXmlElement("ParentNode");
TiXmlElement *newElem = new TiXmlElement("TestNode");
TiXmlText *textElem = new TiXmlText("Test Content");
//textElem->SetCDATA(true);
newElem->LinkEndChild(textElem);
parentElem->LinkEndChild(newElem);
With the line commented I get output XML:
<ParentNode>
<TestNode>Test Content</TestNode>
</ParentNode>
Uncommenting the line I get:
<ParentNode>
<TestNode>
<![CDATA[Test Content]]>
</TestNode>
</ParentNode>
Now ideally it would still all be one line, but I don't really mind it putting the CDATA content nested... but the fact indentation is screwed up on the closing <TestNode> is a pain. Is this a controllable part of TinyXml, or a bug, or just the way it is?

Looks like a bug in the output formatting if TinyXML.
It may work to use TiXmlPrinter for output instead of the TiXmlDocument output (which I assume you are using?). TiXmlPrinter uses a different print path, and may not have the same bug.

Related

MigraDoc - extra and unwanted NewLines

I have a code, where I create a paragraph with 4 lines, 3 of them use tabs to separate row labels and parameter. Astonishingly, a code that worked flawlessly in another form is giving me big headache in current form, even though it is practically 100% identical. Two extra lines appear between line label and a parameter.
Dim ParPara As New Paragraph ' create new paragraph
Dim formtext As New FormattedText
formtext.Bold = True
formtext.AddText("ABC 12345")
Dim formtext2 As New FormattedText
formtext2.Bold = True
formtext2.AddText("163658468435831484")
Dim formtext3 As New FormattedText
formtext3.Bold = True
formtext3.AddText("PARAMETER 1")
ParPara.Add(formtext4)
ParPara.AddLineBreak() ' intentional line break at the end of line
ParPara.AddText("Row Header 1:")
ParPara.AddTab()
ParPara.Add(formtext)
ParPara.AddLineBreak() ' intentional line break at the end of line
ParPara.AddText("Row Header 2:")
ParPara.AddTab()
ParPara.Add(formtext2)
ParPara.AddLineBreak() ' intentional line break at the end of line
ParPara.AddText("Row Header 3:")
ParPara.AddTab()
ParPara.Add(formtext3)
...
TableRowHead0F.Cells(1).Add(ParPara)
I even compared in-memmory content of the paragraph and it's 100% identical in both forms, one working flawlessly and another one inserting extra NewLine breaks:
The in-memmory structure is correct:
The migradoc styles are set identically in both forms (working and failing) too.
Anyone has any ideas why this is happenning?
EDIT: As suggested, I compared MDDDLs and I see no differences:
I don't know, if there can be some context (cell style?) affecting this, but it's really odd. Not the first time I see something like this with MigraDoc...
EDIT 2:
The problems was in switching an order of lines of definition of one style, I had 2nd line of this code at the end:
Dim sStyle As Style = PruvDoc.Styles(StyleNames.Normal)
sStyle = PruvDoc.Styles.AddStyle("Quest", "Normal")
sStyle.ParagraphFormat.TabStops.Clear()
sStyle.ParagraphFormat.TabStops.AddTabStop("9cm", TabAlignment.Left, TabLeader.Dots)
...and that meant that tab stops were applied to the "Normal" style, instead "Quest". Against expectation, it was defined well after the part, where the problems ocured(good to know).
A useful approach to investigate such problems: You can create MDDDL files for the version that works and for the version that does not work and then compare the MDDDL files.
See here:
http://pdfsharp.net/wiki/MigraDocDDL.ashx
MDDDL files are somewhat readable and can be used to check what you actually add to your document - even if you only have a version that does not work as expected.

Qt : Reading the text file and Displaying in LineEdit

I have an input file and a batch file. When the batch file is executed using the System command,
a corresponding outfile is generated.
Now I want a particular text (position 350 to 357) from that outfile to be displayed on to my lineedit widget
Here is that part of my code:
system("C:/ORG_Class0178.bat")
Now the outfile will be generated
File.open("C:/ORG_Class0178_out.txt", 'r').each do |line|
var = line[350..357]
puts var
# To test whether the file is being read.
#responseLineEdit = Qt::LineEdit.new(self)
#responseLineEdit.setFont Qt::Font.new("Times NEw Roman", 12)
#responseLineEdit.resize 100,20
#responseLineEdit.move 210,395
#responseLineEdit.setText("#{var}")
end
When I do test whether the file is being read using puts statement, I get the exact required output in editor. However, the same text is not being displayed on LineEdit. Suggestions are welcome.
EDIT: A wired observation here. It works fine when I try to read the input file and display it , however it does not work with the output file. The puts statement does give the answer in editor confirming that output file does contain the required text. I am confused over this scenario.
There is nothing wrong with the code fragments shown.
Note that var is a local variable. Are the second and third code fragments in the same context? If they are in the same method, and var is not touched in-between, it will work.
If the fragments belong to different methods of the same class, than an instance variable (#var) will solve the problem.
If all that does not help, use Pry to chase the problem. Follow the link to find the pre-requisites and how to use. Place binding.pry in your code, and your program will stop at that line. Then inspect what your variables are doing.
try 'rb' instead of 'r'
File.open("C:/ORG_Class0178_out.txt", 'rb').each do |line|
var = line[350..357]
puts var

What's a reasonable way to read an entire text file as a single string?

I am sure this is an easy one; I just couldn't find the answer immediately from Google.
I know I could do this (right?):
text = ""
File.open(path).each_line do |line|
text += line
end
# Do something with text
But that seems a bit excessive, doesn't it? Or is that the way one would do it in Ruby?
IO.read() is what you're looking for.
File is a subclass of IO, so you may as well just use:
text = File.read(path)
Can't get more intuitive than that.
What about IO.read()?
Edit: IO.read(), as an added bonus, closes the file for you.
First result I found when searching.
I wanted to change the mode, which doesn't seem possible with IO.read, unless I'm wrong?
Anyway, you can do this:
data = File.open(path,'rb',&:read)
It's also good for when you want to use any of the other options:
https://ruby-doc.org/core/IO.html#method-c-new

REXML is wrapping long lines. How do I switch that off?

I am creating an XML document using REXML
File.open(xmlFilename,'w') do |xmlFile|
xmlDoc = Document.new
# add stuff to the document...
xmlDoc.write(xmlFile,4)
end
Some of the elements contain quite a few arguments and hence, the according lines can get quite long. If they get longer than 166 chars, REXML inserts a line break. This is of course still perfectly valid XML, but my workflow includes some diffing and merging, which works best if each element is contained in one line.
So, is there a way to make REXML not insert these line-wrapping line breaks?
Edit: I ended up pushing the finished XML file through tidy as the last step of my script. If someone knew a nicer way to do this, I would still be grateful.
As Ryan Calhoun said in his previous answer, REXML uses 80 as its wrap line length. I'm pretty sure this is a bug (although I couldn't find a bug report just now). I was able to fix it by overwriting the Formatters::Pretty class's write_text method so that it uses the configurable #width attribute instead of the hard-coded 80.
require "rubygems"
require "rexml/document"
include REXML
long_xml = "<root><tag>As Ryan Calhoun said in his previous answer, REXML uses 80 as its wrap line length. I'm pretty sure this is a bug (although I couldn't find a bug report just now). I was able to *fix* it by overwriting the Formatters::Pretty class's write_text method.</tag></root>"
xml = Document.new(long_xml)
#fix bug in REXML::Formatters::Pretty
class MyPrecious < REXML::Formatters::Pretty
def write_text( node, output )
s = node.to_s()
s.gsub!(/\s/,' ')
s.squeeze!(" ")
#The Pretty formatter code mistakenly used 80 instead of the #width variable
#s = wrap(s, 80-#level)
s = wrap(s, #width-#level)
s = indent_text(s, #level, " ", true)
output << (' '*#level + s)
end
end
printer = MyPrecious.new(5)
printer.width = 1000
printer.compact = true
printer.write(xml, STDOUT)
Short answer: yes and no.
REXML uses different formatters based on the value you specify for indent. If you leave the default -1, it uses REXML::Formatters::Default. If you give it a value like 4, it uses REXML::Formatters::Pretty. The pretty formatter does have logic in it to wrap lines (though it looks like it wraps at 80, not 166), when dealing with text (not tags or attributes). For example, the contents of
<p> a paragraph tag </p>
would be wrapped at 80 characters, but
<a-tag with='a' long='list' of='attributes'/>
would not be wrapped.
Anyway the 80 is hard-coded in rexml/formatters/pretty.rb and not configurable. And if you use the default formatter with no indent, then it's mostly just a raw dump without added line breaks. You could try the transitive formatter (see docs for Document.write), but it's broken in some version of ruby and might require a code hack. It probably isn't what you want anyway.
You might try taking a look at Builder::XmlMarkup from the builder gem.

New line or Carraige Return syntax problems

Im pretty new to coding, heres my problem.
Results->Text = "G55 > Y" + System::Convert::ToString(destY);
"Results" is a System.Windows.Forms.Textbox "multiline btw", or so says VS.
That line works fine, but i need a "new line or CR" at the end, so that i can repeat that line with different Literals and a different var in ToString.
For days now ive tried different syntax's ive read about, and i cant get it to take any of them.
Or even a complete different way to input text into Results->Text that would allow for tidy multiline use.
Sidenote: since im using ->Text and System::Convert::ToString in VC, would this code be considered just c++ or .net or CLI? to tighten my searches.
Have you tried System::Environment::NewLine? This will give you CrLf on Windows and whatever is correct for Linux/OS X on those platforms.
Being completely unfamiliar with .NET, I could be completely wrong, but surely adding a + "\n" to the end of your line would do the job? Or failing that, a + "\r\n"?

Resources