Example to reproduce:
docNode = xmlread('FileName.xml')
xmlwrite('FileName.xml', docNode)
-----------------------------------
% Set tempFileName
tempFileName = [tempname, '.xml'];
% Create XML document
docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
docRootNode = docNode.getDocumentElement;
myNode = docNode.createElement('myNode');
docRootNode.appendChild(myNode);
xmlwrite(tempFileName, docNode); edit(tempFileName);
%re-load tempFileName, then re-save it.
docNode = xmlread(tempFileName);
xmlwrite(tempFileName, docNode); edit(tempFileName);
-----------------------------------
First, the xml file produced is:
-----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<root_element>
<myNode/>
</root_element>
-----------------------------------
And then, when it is read back in and then written, the xml produced
is:
-----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<root_element>
<myNode/>
</root_element>
-----------------------------------
Thanks for the help,
Kevin
Hello Kevin,
I have also encountered this issue, and wasn't able to find a fix, or a reason why this happens.
However, I did manage to gather from several other posts a workaround that does the trick for me, it includes writing the document object to a string and then to file, with an intermediate step of fixing an encoding issue,
I summarized the steps in a function you can use which replaces the standard matlab xmlwrite function.
Hope this helps (if still relevant...:))
function ST = myXMLwrite(fileName, docNode)
docNodeRoot=docNode.getDocumentElement;
str=strrep(char(docNode.saveXML(docNodeRoot)), 'encoding="UTF-16"', 'encoding="UTF-8"');
fid=fopen(fileName, 'w');
fwrite(fid, str);
ST = fclose(fid);
Save the above function to myXMLwrite.m
then use as follows:
docNode=xmlread('somefile.xml);
%%%%%%%%%
% Do XML stuff %
%%%%%%%%%
myXMLwrite('somefile.xml', docNode);
Searched and Searched to no avail. This is what I currently use:
fileNode = xmlread(xmlFileName);
rootNode = cleanXML(fileNode.getDocumentElement);
function node = cleanXML(node)
% removes the whitespace nodes from matlab xmlread
for x = 1:node.getLength
child=node.item(x-1);
if(isa(child,'org.apache.xerces.dom.DeferredTextImpl'))
node.removeChild(child);
% element removed, length is no longer valid -- recurse
cleanXML(node);
break;
else
cleanXML(child);
end
end
rootnode = cleanXML(docNode.getDocumentElement)
xmlwrite('FileName.xml', docNode)
Added a new function that operates on the output string:
function XMLstring = xmlwrite_spec(varargin)
% treat input
if nargin==1
writeFlag = false;
FileName = '';
DOMnode = varargin{1};
elseif nargin==2
writeFlag = true;
FileName = varargin{1};
DOMnode = varargin{2};
else
error('Wrong number of arguments')
end
% treat output of xmlwrite
XMLstring = xmlwrite(DOMnode);
while 1
length_Old = length(XMLstring);
XMLstring = strrep(XMLstring,[char(32) char(10)],char(10));
if length_Old==length(XMLstring)
break
end
end
XMLstring = strrep(XMLstring,[char(10) char(10)],char(10));
% if demanded, write result to file
if writeFlag
FID = fopen(FileName,'w');
fwrite(FID,XMLstring);
fclose(FID);
end
I can't think of any other way to identify the pesky whitespace.
They don't seem to be consistent length or have other uniquely identifiable properties.
Any ideas?
Adam
"Alex " <kimm...@uregina.ca> wrote in message <im0ns9$dlk$1...@fred.mathworks.com>...