How do I return CDATA content from XML using IXMLNode (delphi exception)Using a COM DLL in delphi - Access violation in MSVCR80D.dll errorHow to raise exceptions in Delphi?Compare Delphi Exception HandlersEProgrammerNotFound exception in Delphi?Delphi, soap and wrapping values in cdataHow to return array from a Delphi function?Delphi and XML CDATAPost values in HTMLforms without using TwebBrowserHow to affect Delphi XEx code generation for Android/ARM targets?Delphi raise exception in constructor
Do Klingons have escape pods?
Why Doesn't a Bootable USB Boot
How to equalize the chance of throwing the highest dice? (Riddle)
What is the quickest way to raise Affection?
How can medieval knights protect themselves against modern guns?
Memory models for assembly libraries for Turbo C
How can the mechanism of electrons in an atom be explained?
"выше" - adverb or an adjective?
Why I am not getting True when testing equation?
Because things smell, is everything evaporating?
In a world where Magic steam Engines exist what would keep people from making cars
How can you weaponize a thermos?
Is there a sonic boom when flying nearby?
Is Fox News not classified as a news channel?
Do gray aliens exist in Star Trek?
How can I obtain a Cauchy distribution from two standard normal distributions?
How to evaluate math equation, one per line in a file?
Can Vice President Pence be impeached before President Trump?
What is the difference between "cat < filename" and "cat filename"?
Why would a berry have a slow-acting poison?
Did any of the Space Shuttles land through rain or rainclouds?
Someone said to me, "We basically literally did." What were they trying to express to me?
Match blood types in C
Why are Trump's handwritten notes being focused on in the news?
How do I return CDATA content from XML using IXMLNode (delphi exception)
Using a COM DLL in delphi - Access violation in MSVCR80D.dll errorHow to raise exceptions in Delphi?Compare Delphi Exception HandlersEProgrammerNotFound exception in Delphi?Delphi, soap and wrapping values in cdataHow to return array from a Delphi function?Delphi and XML CDATAPost values in HTMLforms without using TwebBrowserHow to affect Delphi XEx code generation for Android/ARM targets?Delphi raise exception in constructor
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I have an XML document I try to extract values from using Delphi, and XMLDoc. Most parts work without problem. I use IXMLNode to select the node text. But I have one section which may contain CDATA. This always throws an exception when I try to get it.
My XML (relevant part) is similar to this
<a>valuea</a>
<b>My b value</b>
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
My code today is something like this:
var
IDoc: IXMLDocument;
INode: IXMLNode;
XPathText : string;
i : integer;
// From a post in Embarcadero's Delphi XML forum.
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
intfSelect : IDomNodeSelect;
dnResult : IDomNode;
intfDocAccess : IXmlDocumentAccess;
doc: TXmlDocument;
begin
Result := nil;
if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
Exit;
dnResult := intfSelect.selectNode(nodePath);
if Assigned(dnResult) then
begin
if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
doc := intfDocAccess.DocumentObject
else
doc := nil;
Result := TXmlNode.Create(dnResult, nil, doc);
end;
end;
// --------------------------------------------
begin
IDoc:= LoadXMLDocument(edtXMLFileName.Text);
idoc.ParseOptions := [poPreserveWhiteSpace];
XPathText := './/path/to/c'; // as per example above, this is my CDATA
INode := selectnode(IDoc.DocumentElement, xpathtext);
showmessage(inode.text); // <<< Notice: .text not .XML FAILS for XML with exception.
end;
What is the correct way to
- Get the CDATA value
- ..while keeping any linefeeds
- I do not need tag or the CDATA tag. Just content.
EDIT
Current state (correction): If I use the .XML value of the IXMLNode, I get the full tag including the CDATA etc:
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
but if I use the .text, delphi throws an exception.
"Element does not contain a single text node."
Someone suggested here to use XMLTextReader, but I need to query here and there and can't do a forward read only.
My backup plan would be to use a separate function to just remove the XML / CDATA tag returned as above, but it's not pretty.
delphi
add a comment
|
I have an XML document I try to extract values from using Delphi, and XMLDoc. Most parts work without problem. I use IXMLNode to select the node text. But I have one section which may contain CDATA. This always throws an exception when I try to get it.
My XML (relevant part) is similar to this
<a>valuea</a>
<b>My b value</b>
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
My code today is something like this:
var
IDoc: IXMLDocument;
INode: IXMLNode;
XPathText : string;
i : integer;
// From a post in Embarcadero's Delphi XML forum.
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
intfSelect : IDomNodeSelect;
dnResult : IDomNode;
intfDocAccess : IXmlDocumentAccess;
doc: TXmlDocument;
begin
Result := nil;
if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
Exit;
dnResult := intfSelect.selectNode(nodePath);
if Assigned(dnResult) then
begin
if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
doc := intfDocAccess.DocumentObject
else
doc := nil;
Result := TXmlNode.Create(dnResult, nil, doc);
end;
end;
// --------------------------------------------
begin
IDoc:= LoadXMLDocument(edtXMLFileName.Text);
idoc.ParseOptions := [poPreserveWhiteSpace];
XPathText := './/path/to/c'; // as per example above, this is my CDATA
INode := selectnode(IDoc.DocumentElement, xpathtext);
showmessage(inode.text); // <<< Notice: .text not .XML FAILS for XML with exception.
end;
What is the correct way to
- Get the CDATA value
- ..while keeping any linefeeds
- I do not need tag or the CDATA tag. Just content.
EDIT
Current state (correction): If I use the .XML value of the IXMLNode, I get the full tag including the CDATA etc:
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
but if I use the .text, delphi throws an exception.
"Element does not contain a single text node."
Someone suggested here to use XMLTextReader, but I need to query here and there and can't do a forward read only.
My backup plan would be to use a separate function to just remove the XML / CDATA tag returned as above, but it's not pretty.
delphi
What exception are you getting exactly? On which line?
– Remy Lebeau
Mar 29 at 4:46
Remy: See above, I edited the question for clarity. I can either get the full "c" tag or get an exception.
– MyICQ
Mar 29 at 7:10
add a comment
|
I have an XML document I try to extract values from using Delphi, and XMLDoc. Most parts work without problem. I use IXMLNode to select the node text. But I have one section which may contain CDATA. This always throws an exception when I try to get it.
My XML (relevant part) is similar to this
<a>valuea</a>
<b>My b value</b>
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
My code today is something like this:
var
IDoc: IXMLDocument;
INode: IXMLNode;
XPathText : string;
i : integer;
// From a post in Embarcadero's Delphi XML forum.
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
intfSelect : IDomNodeSelect;
dnResult : IDomNode;
intfDocAccess : IXmlDocumentAccess;
doc: TXmlDocument;
begin
Result := nil;
if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
Exit;
dnResult := intfSelect.selectNode(nodePath);
if Assigned(dnResult) then
begin
if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
doc := intfDocAccess.DocumentObject
else
doc := nil;
Result := TXmlNode.Create(dnResult, nil, doc);
end;
end;
// --------------------------------------------
begin
IDoc:= LoadXMLDocument(edtXMLFileName.Text);
idoc.ParseOptions := [poPreserveWhiteSpace];
XPathText := './/path/to/c'; // as per example above, this is my CDATA
INode := selectnode(IDoc.DocumentElement, xpathtext);
showmessage(inode.text); // <<< Notice: .text not .XML FAILS for XML with exception.
end;
What is the correct way to
- Get the CDATA value
- ..while keeping any linefeeds
- I do not need tag or the CDATA tag. Just content.
EDIT
Current state (correction): If I use the .XML value of the IXMLNode, I get the full tag including the CDATA etc:
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
but if I use the .text, delphi throws an exception.
"Element does not contain a single text node."
Someone suggested here to use XMLTextReader, but I need to query here and there and can't do a forward read only.
My backup plan would be to use a separate function to just remove the XML / CDATA tag returned as above, but it's not pretty.
delphi
I have an XML document I try to extract values from using Delphi, and XMLDoc. Most parts work without problem. I use IXMLNode to select the node text. But I have one section which may contain CDATA. This always throws an exception when I try to get it.
My XML (relevant part) is similar to this
<a>valuea</a>
<b>My b value</b>
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
My code today is something like this:
var
IDoc: IXMLDocument;
INode: IXMLNode;
XPathText : string;
i : integer;
// From a post in Embarcadero's Delphi XML forum.
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
intfSelect : IDomNodeSelect;
dnResult : IDomNode;
intfDocAccess : IXmlDocumentAccess;
doc: TXmlDocument;
begin
Result := nil;
if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
Exit;
dnResult := intfSelect.selectNode(nodePath);
if Assigned(dnResult) then
begin
if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
doc := intfDocAccess.DocumentObject
else
doc := nil;
Result := TXmlNode.Create(dnResult, nil, doc);
end;
end;
// --------------------------------------------
begin
IDoc:= LoadXMLDocument(edtXMLFileName.Text);
idoc.ParseOptions := [poPreserveWhiteSpace];
XPathText := './/path/to/c'; // as per example above, this is my CDATA
INode := selectnode(IDoc.DocumentElement, xpathtext);
showmessage(inode.text); // <<< Notice: .text not .XML FAILS for XML with exception.
end;
What is the correct way to
- Get the CDATA value
- ..while keeping any linefeeds
- I do not need tag or the CDATA tag. Just content.
EDIT
Current state (correction): If I use the .XML value of the IXMLNode, I get the full tag including the CDATA etc:
<c>![CDATA[My cdata text goes here
It may have linefeed inside
like this
and I need to get all lines WITH linefeeds
]]>
</c>
but if I use the .text, delphi throws an exception.
"Element does not contain a single text node."
Someone suggested here to use XMLTextReader, but I need to query here and there and can't do a forward read only.
My backup plan would be to use a separate function to just remove the XML / CDATA tag returned as above, but it's not pretty.
delphi
delphi
edited Mar 29 at 7:08
MyICQ
asked Mar 28 at 22:04
MyICQMyICQ
436 bronze badges
436 bronze badges
What exception are you getting exactly? On which line?
– Remy Lebeau
Mar 29 at 4:46
Remy: See above, I edited the question for clarity. I can either get the full "c" tag or get an exception.
– MyICQ
Mar 29 at 7:10
add a comment
|
What exception are you getting exactly? On which line?
– Remy Lebeau
Mar 29 at 4:46
Remy: See above, I edited the question for clarity. I can either get the full "c" tag or get an exception.
– MyICQ
Mar 29 at 7:10
What exception are you getting exactly? On which line?
– Remy Lebeau
Mar 29 at 4:46
What exception are you getting exactly? On which line?
– Remy Lebeau
Mar 29 at 4:46
Remy: See above, I edited the question for clarity. I can either get the full "c" tag or get an exception.
– MyICQ
Mar 29 at 7:10
Remy: See above, I edited the question for clarity. I can either get the full "c" tag or get an exception.
– MyICQ
Mar 29 at 7:10
add a comment
|
1 Answer
1
active
oldest
votes
A CDATA node is different from a Text node. You can't use the IXMLNode.Text
property to read the content of a CDATA node. This is documented behavior:
Text
is intended for use nodes where theIsTextElement
property is true. IfIsTextElement
is false, then if the node has no children, the value ofText
is an empty string.
Setting
Text
is this case result in a node whereIsTextElement
is true.
If the node has children (other than a single DOM text node), reading or setting
Text
causes an exception.
You need to use the IXMLNode.NodeValue
property instead, which can read both CDATA and Text content:
ShowMessage(INode.NodeValue);
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407555%2fhow-do-i-return-cdata-content-from-xml-using-ixmlnode-delphi-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A CDATA node is different from a Text node. You can't use the IXMLNode.Text
property to read the content of a CDATA node. This is documented behavior:
Text
is intended for use nodes where theIsTextElement
property is true. IfIsTextElement
is false, then if the node has no children, the value ofText
is an empty string.
Setting
Text
is this case result in a node whereIsTextElement
is true.
If the node has children (other than a single DOM text node), reading or setting
Text
causes an exception.
You need to use the IXMLNode.NodeValue
property instead, which can read both CDATA and Text content:
ShowMessage(INode.NodeValue);
add a comment
|
A CDATA node is different from a Text node. You can't use the IXMLNode.Text
property to read the content of a CDATA node. This is documented behavior:
Text
is intended for use nodes where theIsTextElement
property is true. IfIsTextElement
is false, then if the node has no children, the value ofText
is an empty string.
Setting
Text
is this case result in a node whereIsTextElement
is true.
If the node has children (other than a single DOM text node), reading or setting
Text
causes an exception.
You need to use the IXMLNode.NodeValue
property instead, which can read both CDATA and Text content:
ShowMessage(INode.NodeValue);
add a comment
|
A CDATA node is different from a Text node. You can't use the IXMLNode.Text
property to read the content of a CDATA node. This is documented behavior:
Text
is intended for use nodes where theIsTextElement
property is true. IfIsTextElement
is false, then if the node has no children, the value ofText
is an empty string.
Setting
Text
is this case result in a node whereIsTextElement
is true.
If the node has children (other than a single DOM text node), reading or setting
Text
causes an exception.
You need to use the IXMLNode.NodeValue
property instead, which can read both CDATA and Text content:
ShowMessage(INode.NodeValue);
A CDATA node is different from a Text node. You can't use the IXMLNode.Text
property to read the content of a CDATA node. This is documented behavior:
Text
is intended for use nodes where theIsTextElement
property is true. IfIsTextElement
is false, then if the node has no children, the value ofText
is an empty string.
Setting
Text
is this case result in a node whereIsTextElement
is true.
If the node has children (other than a single DOM text node), reading or setting
Text
causes an exception.
You need to use the IXMLNode.NodeValue
property instead, which can read both CDATA and Text content:
ShowMessage(INode.NodeValue);
edited Mar 29 at 17:56
answered Mar 29 at 17:50
Remy LebeauRemy Lebeau
372k22 gold badges296 silver badges506 bronze badges
372k22 gold badges296 silver badges506 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407555%2fhow-do-i-return-cdata-content-from-xml-using-ixmlnode-delphi-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What exception are you getting exactly? On which line?
– Remy Lebeau
Mar 29 at 4:46
Remy: See above, I edited the question for clarity. I can either get the full "c" tag or get an exception.
– MyICQ
Mar 29 at 7:10