How to read a string in XML with CDATA

efassettaefassetta Member Posts: 53
edited 2014-10-25 in NAV Three Tier
Hello to everybody,

my question is simple: i need to read the CDATA string into dataOra node ...

<?xml version="1.0" encoding="UTF-8"?>
-<lettera>
<type>L1/L2</tipo>
-<dataOra><![CDATA[Verona, li 21/10/2014]]></dataOra>
</lettera>

I created a codeunit exposed, with a method with a input parameter, a bigtext variable by ref.

In this way, a website call the cu exposed, passing the variable filled, ready to be processed by Nav.

I'm using XMLDOM.

I choice to use CDATA, because in this way it's possible (maybe..) to manage also character not supported such as àòù ....

Thank's to everybody.

Egon

Comments

  • PrebenRasmussenPrebenRasmussen Member Posts: 137
    You can use a specific CDATASection for this.
    ('Microsoft XML, v6.0'.IXMLDOMCDATASection)
    CDATASection := DOMDocument.createCDATASection(data);
    REPEAT
      CDATASection.appenddata(data)
    UNTIL NoMoreText...
    SomeNode.appendChild(CDATASection);
    
  • efassettaefassetta Member Posts: 53
    You can use a specific CDATASection for this.
    ('Microsoft XML, v6.0'.IXMLDOMCDATASection)
    CDATASection := DOMDocument.createCDATASection(data);
    REPEAT
      CDATASection.appenddata(data)
    UNTIL NoMoreText...
    SomeNode.appendChild(CDATASection);
    

    Great !

    thank's a lot for your answer ...

    So i have the code below

    CREATE(XmlDoc);
    TempBlob.INIT;
    TempBlob.Blob.CREATEOUTSTREAM(writeStream);
    XMLInput.WRITE(writeStream);
    TempBlob.CALCFIELDS(Blob);
    TempBlob.Blob.CREATEINSTREAM(readStream);
    XmlDoc.load(readStream);
    XmlDomRoot := XmlDoc.documentElement;

    Type := XmlDomRoot.getElementsByTagName('type').item(0).text;

    where
    XmlDoc 'Microsoft XML, v6.0'.DOMDocument60
    XmlDomRoot 'Microsoft XML, v6.0'.IXMLDOMElement
    XMLInput:

    <letter>
    <type>L1/L2</type>
    <dataOra><![CDATA[Verona, li 21/10/2014]]></dataOra>
    </letter>

    i declared as you suggested a variable 'Microsoft XML, v6.0'.IXMLDOMCDATASection, but in my sample, how can i read the content of dataOra node?

    Thank's a lot for your help....

    Egon
  • PrebenRasmussenPrebenRasmussen Member Posts: 137
    You wrote "into" in your original post. So I thought you wanted to fill the section with data.
  • efassettaefassetta Member Posts: 53
    You wrote "into" in your original post. So I thought you wanted to fill the section with data.
    Ah, sorry ...

    I thought it was clear, because i wrote also that i need to read the CDATA string, not to write .... :)

    It's a pity .... :( I hoped in your solution ...

    Thank's a lot however ...

    Egon
  • PrebenRasmussenPrebenRasmussen Member Posts: 137
    Well no worries. If you want to read the section content - Then do something like this:
    CDATASection := NodeDataOra.ChildNodes[0];
    Len := CDATASection.Length;
    IF Len > 0 THEN
      REPEAT
        Data := CDATASection.substringData(StartPos, MAXSTRLEN(Data));
        BigText.ADDTEXT(Data);
        StartPos += STRLEN(Data);
      UNTIL StartPos = Len;
    
    You might see that sometimes NodeDataOra has several childnodes so you should check for this.
  • efassettaefassetta Member Posts: 53
    Well no worries. If you want to read the section content - Then do something like this:
    CDATASection := NodeDataOra.ChildNodes[0];
    Len := CDATASection.Length;
    IF Len > 0 THEN
      REPEAT
        Data := CDATASection.substringData(StartPos, MAXSTRLEN(Data));
        BigText.ADDTEXT(Data);
        StartPos += STRLEN(Data);
      UNTIL StartPos = Len;
    
    You might see that sometimes NodeDataOra has several childnodes so you should check for this.

    Thank's, i tried and it's WORK fine ... :)

    Below my code where:

    XML is the follow const:
    <letter>
    <type>DAS/DLS</type>
    <dataOra><![CDATA[Verona, li 21/10/2014]]></dataOra>
    </letter>

    XMLBig is a BigText
    XMLDoc is 'Microsoft XML, v6.0'.DOMDocument60
    XMLDomRoot is 'Microsoft XML, v6.0'.IXMLDOMElement
    XMLCDATA is 'Microsoft XML, v6.0'.IXMLDOMCDATASection
    dataTxt is a string 1024


    XMLBig.ADDTEXT(XML);

    TempBlob.Blob.CREATEOUTSTREAM(OutXMLStream);
    XMLBig.WRITE(OutXMLStream);
    TempBlob.CALCFIELDS(Blob);
    TempBlob.Blob.CREATEINSTREAM(InXmlStream);

    CLEAR(XmlDoc);
    CREATE(XmlDoc);
    XmlDoc.load(InXmlStream);

    XmlDomRoot := XmlDoc.documentElement;
    XMLCDATA := XmlDoc.createCDATASection(XmlDomRoot.getElementsByTagName('dataOra').item(0).text);
    Len := XMLCDATA.length;
    IF Len > 0 THEN
    REPEAT
    dataTxt := XMLCDATA.substringData(StartPos, MAXSTRLEN(dataTxt));
    StartPos += STRLEN(dataTxt);
    UNTIL StartPos = Len;
  • cafeinecafeine Member Posts: 11
    Well no worries. If you want to read the section content - Then do something like this:
    CDATASection := NodeDataOra.ChildNodes[0];
    Len := CDATASection.Length;
    IF Len > 0 THEN
      REPEAT
        Data := CDATASection.substringData(StartPos, MAXSTRLEN(Data));
        BigText.ADDTEXT(Data);
        StartPos += STRLEN(Data);
      UNTIL StartPos = Len;
    
    You might see that sometimes NodeDataOra has several childnodes so you should check for this.

    You are a beautiful man :) thanks for posting this
Sign In or Register to comment.