Problem with comments in XML files

조회 수: 22 (최근 30일)
Bob
Bob 2025년 7월 24일
댓글: Bob 2025년 7월 28일
I have an m-file that reads an XML file to get inputs. I'm running into a problem with comments in the XML file. Simply put, if I put a comment inside the tag but before the value, then I get an empty string for the value. Comments elsewhere don't cause problems
Here's a toy example of my m-file:
function userInputStruct = readToyXmlInput()
toyXmlInputFile = 'toyXmlInput.xml';
xDoc = xmlread(toyXmlInputFile);
userInput = xDoc.getElementsByTagName('userInput');
userInputStruct = struct(); % Initialize
thisUserInput = userInput.item(0);
thisInput_01 = thisUserInput.getElementsByTagName('input_01');
thisInput_01Element = thisInput_01.item(0);
temp01 = thisInput_01Element.getFirstChild.getData;
temp02 = string(temp01);
input_01 = strip(temp02);
userInputStruct.input_01 = input_01;
Here's a toy example of my XML file:
<toyXmlInput>
<userInput>
<!-- A comment here is fine. -->
<input_01>
Dummy input #1 <!-- A comment here is fine. -->
<!-- A comment here is fine. -->
</input_01>
</userInput>
</toyXmlInput>
Here's a toy example of my XML file with a comment that causes a problem.
<toyXmlInput>
<userInput>
<input_01>
<!-- A comment here causes problems -->
Dummy input #1
</input_01>
</userInput>
</toyXmlInput>
What am I doing wrong? What can I do differently so that comments after the tag but before the value don't cause problems?
Worst-case, I'll just document in the m-file and the XML file that there's a limit on where comments can go. However, I'd prefer a more elegant solution.

채택된 답변

Stephen23
Stephen23 2025년 7월 24일
편집: Stephen23 2025년 7월 25일
The issue you're encountering is related to how XML Document Object Model (DOM) parsing handles different types of nodes within an element. When you use getFirstChild, you're getting the very first child node, which could be a text node, comment node, or element node. In your problematic case, the first child is a comment node, not the text content you want.
Here's what's happening:
  1. In your working XML, the first child of <input_01> is a text node (even if it's just whitespace before "Dummy input #1")
  2. In your problematic XML, the first child is a comment node, so getFirstChild.getData returns empty
XML Node Types:
  • Node Type 1: Element node (like <input_01>)
  • Node Type 3: Text node (the actual text content)
  • Node Type 4: CDATA section
  • Node Type 8: Comment node (what's causing your problem)
Solution:
Use getTextContent() - this is the simplest and most elegant solution. Replace your current line:
temp01 = thisInput_01Element.getFirstChild.getData;
With:
temp01 = thisInput_01Element.getTextContent;
The getTextContent() method automatically:
  • Concatenates all text content within the element
  • Ignores comments, processing instructions, and other non-text nodes
  • Handles nested elements properly
The getTextContent() approach will work with both of your XML examples and is the standard way to extract text content from XML elements while ignoring comments.
  댓글 수: 1
Bob
Bob 2025년 7월 24일
Thanks! I obviously didn't know about getTextContent(). That method's doing exactly what I want and need.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2025년 7월 24일
Do you need the comments to be imported? If not, and if upgrading to release R2020b or later is an option, consider using readstruct to read the contents of the XML file into a struct array. I've attached xmlfile1.txt and xmlfile2.txt as your two toy XML files. [I can't attach the files as .xml.] But I copy them into XML files with the first two lines.
copyfile xmlfile1.txt xmlfile1.xml
copyfile xmlfile2.txt xmlfile2.xml
The comments are not present in the data1 and data2 struct arrays, but the data is present.
data1 = readstruct('xmlfile1.xml')
data1 = struct with fields:
userInput: [1×1 struct]
data1.userInput
ans = struct with fields:
input_01: "Dummy input #1"
data2 = readstruct('xmlfile2.xml')
data2 = struct with fields:
userInput: [1×1 struct]
data2.userInput
ans = struct with fields:
input_01: "Dummy input #1"
  댓글 수: 3
Stephen23
Stephen23 2025년 7월 26일
편집: Stephen23 2025년 7월 26일
Use the Contents browser on the left hand side to navigate the documentation.
Bob
Bob 2025년 7월 28일
Thanks again!

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

카테고리

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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by