XMLwrite involuntary wrapping of long tags - not behaving as it should (bug) - upgrade from 2018a
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
DanielS
el 21 de Mayo de 2019
Respondida: Josh Kahn
el 10 de Oct. de 2024
Good afternoon,
today I tried to migrate to the new 2019a version.
All my current code ran pretty well as I had hoped, but there is one big issue for me. I write some of my results in XML, and the way XML is handled by Matlab has changed in a way that is undesired for me: It is regards to line breaks. I made a demo script to reproduce the bug so it would be easier to understand what I mean.
Problem: After writing number of attributes on the same tag eventually it will start to wrap them. From that first wrap, it will actually wrap every single attribute line-by-line.
The resulting output is most likely according to XML specs (it can be understood perfectly), but for a human it looks hideous.
After trying to change the bahaviour and playing with some javasettings I gave up and reverted to 2018a.
Imagine a 50-500 tag xml with this behaviour... ¯\_(ツ)_/¯
Does anybody understand what is going on here, why it has changed and if there is a way to mitigate this in the new release?
Thank you for reading
Cheers, Daniel
Script to reproduce the bug:
%% Create Document and Elements
docNode = com.mathworks.xml.XMLUtils.createDocument('Demo');
root = docNode.getDocumentElement; % Identify root element
Content = docNode.createElement('Content');
Analysis = docNode.createElement('Analysis');
Output = docNode.createElement('LongNamedElementWrapsSoonerYoullSee');
% Define the attributes
for i=1:15
Analysis.setAttribute(strcat('t',num2str(i)),'test');
end
for i=1:3
Output.setAttribute(strcat('t',num2str(i)),'AtLeastItWillGetTheTabsRight');
end
% Append everything
root.appendChild(Content);
Content.appendChild(Analysis);
Content.appendChild(Output);
% Write
xmlwrite('Demo.xml',docNode);
2018a output
<?xml version="1.0" encoding="utf-8"?>
<Demo>
<Content>
<Analysis t1="test" t10="test" t11="test" t12="test" t13="test" t14="test" t15="test" t2="test" t3="test" t4="test" t5="test" t6="test" t7="test" t8="test" t9="test"/>
<LongNamedElementWrapsSoonerYoullSee t1="AtLeastItWillGetTheTabsRight" t2="AtLeastItWillGetTheTabsRight" t3="AtLeastItWillGetTheTabsRight"/>
</Content>
</Demo>
2019a output
<?xml version="1.0" encoding="utf-8"?>
<Demo>
<Content>
<Analysis t1="test" t10="test" t11="test" t12="test" t13="test" t14="test" t15="test"
t2="test"
t3="test"
t4="test"
t5="test"
t6="test"
t7="test"
t8="test"
t9="test"/>
<LongNamedElementWrapsSoonerYoullSee t1="AtLeastItWillGetTheTabsRight" t2="AtLeastItWillGetTheTabsRight"
t3="AtLeastItWillGetTheTabsRight"/>
</Content>
</Demo>
2 comentarios
Guillaume
el 21 de Mayo de 2019
I'm not sure you can call this a bug. As you've pointed out, from an xml point of view, the two outputs are identical. And as the actual formatting of the xml is not documented at all, you shouldn't have any expectation of that formatting. Personally, I would have expected the xml to be just one long line.
You can always run the file through a beautifier. But this also begs the question: why is a human looking at the raw xml?
Respuesta aceptada
Josh Kahn
el 10 de Oct. de 2024
Here are the changes that should achieve your expected behavior.
Hope this helps,
Josh
%% Create Document and Elements
docNode = matlab.io.xml.dom.Document('Demo'); %% CHANGED
root = docNode.getDocumentElement; % Identify root element
Content = docNode.createElement('Content');
Analysis = docNode.createElement('Analysis');
Output = docNode.createElement('LongNamedElementWrapsSoonerYoullSee');
% Define the attributes
for i=1:15
Analysis.setAttribute(strcat('t',num2str(i)),'test');
end
for i=1:3
Output.setAttribute(strcat('t',num2str(i)),'AtLeastItWillGetTheTabsRight');
end
% Append everything
root.appendChild(Content);
Content.appendChild(Analysis);
Content.appendChild(Output);
% Write
writer = matlab.io.xml.dom.DOMWriter; %% CHANGED
writer.Configuration.FormatPrettyPrint = true; %% CHANGED
writeToFile(writer,docNode,'Demo.xml'); %% CHANGED
0 comentarios
Más respuestas (2)
Guillaume
el 21 de Mayo de 2019
and I have relied myself upon this, no wrong in that
Well, you're relying on undocumented behaviour. It's always risky and you can't really complain when it changes. Now if it were documented, then yes, you could complain loudly.
In any case, it looks like the behaviour is outside Mathworks control. You can look at the source code of xmlwrite and you'll see that it relies on Java for everything. The only thing that is matlab dependent is a XSLT file (in matlabroot\toolbox\matlab\iofun\+matlab\+io\+internal) which I don't believe has any effect on what you're seeing. So it looks to me that if a change has occured, it's in Java.
I've no longer got 2018a installed. 2018b and 2019a produce the same output.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!