XML special characters

조회 수: 9 (최근 30일)
Joan Puig
Joan Puig 2011년 9월 16일
Hi,
I have some code that generates an xml string. For some reason, when I try to use special characters the xmlread function is not able to read it back in:
clear();
clc();
docFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
stuff = doc.createElement('stuff');
stuff.setAttribute('value','ó'); % If a regular character is used, it works fine
doc.appendChild(stuff);
factory = javax.xml.transform.TransformerFactory.newInstance();
transformer = factory.newTransformer();
writer = java.io.StringWriter();
result = javax.xml.transform.stream.StreamResult(writer);
source = javax.xml.transform.dom.DOMSource(doc);
transformer.transform(source, result);
writer.close();
xml = char(writer.toString())
jString = java.lang.String(xml);
readOut = xmlread(java.io.ByteArrayInputStream(jString.getBytes()));
This is the xml string that gets generated:
xml =
<?xml version="1.0" encoding="utf-8"?><stuff value="ó"/>
This is the error it gives me:
[Fatal Error] :1:53: Invalid byte 2 of 4-byte UTF-8 sequence.
??? Java exception occurred:
org.xml.sax.SAXParseException: Invalid byte 2 of 4-byte UTF-8 sequence.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
Error in ==> xmlread at 98
parseResult = p.parse(fileName);
Error in ==> scr_xml_encoding at 20
readOut = xmlread(java.io.ByteArrayInputStream(jString.getBytes()));
>>
Do you have any idea of how to fix it?
Thanks, Joan

답변 (2개)

Walter Roberson
Walter Roberson 2011년 9월 16일
You will probably have to use native2unicode() to build the value to set() as the attribute. In MATLAB, 'ó' is just a 16 bit value, spanning the first 65536 unicode code points; you need to do a UTF-8 encoding of it.
  댓글 수: 1
Joan Puig
Joan Puig 2011년 9월 17일
Hi Walter,
Thanks for your answer. I looks like native2unicode() could be used to get the expected results, but I have tried a few combinations and was not able to get it working. Could you please be a little bit more specific?
Thanks,
Joan

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


Silvia Muñoz Polo
Silvia Muñoz Polo 2013년 9월 11일
Hi, I have the same problem. The native2unicode() function works at Matlab environment but the generated xml file can't be edited. Any idea to solve this problem? Thanks.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by