setXmlStandalone bug?

조회 수: 4 (최근 30일)
Ondrej
Ondrej 2011년 4월 7일
댓글: Isirame Omofuma 2019년 7월 29일
Hi, I have this problem. If I make this in matlab
---
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
docNode.setXmlStandalone(1);
docNode.setXmlVersion('1.1');
xmlWrite('file.xml',docNode);
----
I don't see any "standalone" atttribute, and the version is still set to '1.0'.
i.e. the 'file.xml' looks like:
'<?xml version="1.0" encoding="utf-8"?> root/'
but it should look like:
'<?xml version="1.1" standalone="yes" encoding="utf-8"?> root/
Is it a bug in matlab, or am I doing something wrong? Thanks.
I'm using Matlab 2009b (7.9.0).
  댓글 수: 1
Abby
Abby 2012년 2월 5일
Did you ever find a solution for this? I have the same issue.

댓글을 달려면 로그인하십시오.

채택된 답변

Michael Katz
Michael Katz 2012년 2월 6일
Our implementation doesn't forward that document property to the serializer. You'll have to do it yourself, like this:
docNode = com.mathworks.xml.XMLUtils.createDocument('AddressBook');
%don't do this docNode.setXmlStandalone(1)
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
tfactory = TransformerFactory.newInstance;
serializer = tfactory.newTransformer;
src = DOMSource(docNode);
stream = java.io.StringWriter;
dst = StreamResult(stream);
%set the value here instead
serializer.setOutputProperty(OutputKeys.STANDALONE,'yes');
serializer.setOutputProperty(OutputKeys.VERSION,'1.1');
serializer.transform(src,dst);
result = char(stream.toString)
  댓글 수: 3
Ondrej
Ondrej 2012년 2월 6일
I would say, it is just much more "user-friendly" to write one line of code instead of "low-level xml serializing".
Isirame Omofuma
Isirame Omofuma 2019년 7월 29일
Hi, I did all this and found a way to write the string result to a domnode.
import java.io.StringReader;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
s = StringReader(result);
i = InputSource(s);
doc = builder.parse(i);
xmlwrite('try.xml', doc)
On using xmlwrite to write the result to file the standalone argument still does not appear in the xml file and the version remains as 1.1. Is there a simpler way to write to xml? Is there a simpler way to solve this probelm?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Darlling5147 Sew
Darlling5147 Sew 2013년 12월 6일
how if I wanted to save it into .xml file? I tried this method but at the very last step I wanted to write" xmlwrite('AB.xml',result)" this it fail.....how to solve this problem of exporting into .xml format?
Thank you in advance.
  댓글 수: 1
Ondrej
Ondrej 2013년 12월 6일
xmlwrite requires DOMnode object as input. And not some char. e.g. xmlWrite('AB.xml',docNode)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by