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;









1


















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.










share|improve this question



























  • 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

















1


















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.










share|improve this question



























  • 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













1













1









1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















1



















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 the IsTextElement property is true. If IsTextElement is false, then if the node has no children, the value of Text is an empty string.



Setting Text is this case result in a node where IsTextElement 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);





share|improve this answer





























    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
    );



    );














    draft saved

    draft discarded
















    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









    1



















    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 the IsTextElement property is true. If IsTextElement is false, then if the node has no children, the value of Text is an empty string.



    Setting Text is this case result in a node where IsTextElement 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);





    share|improve this answer
































      1



















      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 the IsTextElement property is true. If IsTextElement is false, then if the node has no children, the value of Text is an empty string.



      Setting Text is this case result in a node where IsTextElement 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);





      share|improve this answer






























        1















        1











        1









        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 the IsTextElement property is true. If IsTextElement is false, then if the node has no children, the value of Text is an empty string.



        Setting Text is this case result in a node where IsTextElement 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);





        share|improve this answer
















        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 the IsTextElement property is true. If IsTextElement is false, then if the node has no children, the value of Text is an empty string.



        Setting Text is this case result in a node where IsTextElement 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);






        share|improve this answer















        share|improve this answer




        share|improve this answer








        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

































            draft saved

            draft discarded















































            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.




            draft saved


            draft discarded














            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





















































            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









            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현