[PB 5.22] Save XML and #LF$ in attribute

Windows specific forum
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

[PB 5.22] Save XML and #LF$ in attribute

Post by Le Soldat Inconnu »

Hi,

I have a problem when i save XML file.

I have a XML file like this one :

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-ME5.xsd">
    <displaySettings displayType="replace" securityCode="*" backColor="#3A7878" maximumTagUpdateRate="0.5" focusHighlightColor="lime" disableFocusHighlight="false" width="800" height="600" displayNumber="1" disableInitialInputFocus="false" startupMacro="" shutdownMacro=""/>
    <text name="Text7" height="312" width="415" left="175" top="165" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleLeft" fontFamily="Arial" fontSize="14" bold="false" italic="false" underline="false" strikethrough="false" caption="Main Information of this part of Machine :&#xA;&#xA;Put a Picture of this part of Machine in Wall Paper&#xA;&#xA;Example : &#xA;&#xA;Information :&#xA; - Cycle Time&#xA; - Current Receipe Id&#xA; - Operator Message&#xA; - ...&#xA;&#xA;Action : &#xA;BP Restart or Next..."/>
</gfx>
In this XML, #LF$ is write &#xA;
look in attribute "caption" of /gfx/text

My problem is when i save this XML after modification, i get :

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-ME5.xsd">
    <displaySettings displayType="replace" securityCode="*" backColor="#3A7878" maximumTagUpdateRate="0.5" focusHighlightColor="lime" disableFocusHighlight="false" width="800" height="600" displayNumber="1" disableInitialInputFocus="false" startupMacro="" shutdownMacro=""/>
    <text name="Text7" height="312" width="415" left="175" top="165" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleLeft" fontFamily="Arial" fontSize="14" bold="false" italic="false" underline="false" strikethrough="false" caption="Main Information of this part of Machine :

Put a Picture of this part of Machine in Wall Paper

Example : 

Information :
 - Cycle Time
 - Current Receipe Id
 - Operator Message
 - ...

Action : 
BP Restart or Next..."/>
</gfx>
So with Chr(10)

And now, i get error when import XML in my big automation project (Rockwell software RS5000 and factory talk view studio)

Other specific caracters, like &, are well formated in &

Thanks
LSI
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB 5.22] Save XML and #LF$ in attribute

Post by Little John »

I think that PB's behaviour here is correct -- no bug.
To achieve what you want, I can imagine two ways (not tested):
  • Do not read the XML file with LoadXML(),
    but open it with ReadFile(), and then load its contents with ReadString() into one string.
    In this string replace e.g. "&#" with "&#" (or "&#xA;" with "&#xA;"), and then use CatchXML() in order to create an XML tree from the XML data in the string.
    Then proceed as usual.

    OR
  • At first proceed as usual (e.g. read the XML file with LoadXML(), and do the desired things with the data.
    At the end, instead of using SaveXML(), write the XML data to a string in menory by using ExportXML().
    In this string, replace #LF$ with "&#xA;", then save this string to a file with CreateFile() and WriteString().

    You can modify my procedure SaveFormattedXml() for this purpose:
    Replace

    Code: Select all

    xml$ = PeekS(*buffer, -1, encoding)
    with

    Code: Select all

    xml$ = ReplaceString(PeekS(*buffer, -1, encoding), #LF$, "&#xA;")
    Then instead of SaveXML(), just use SaveFormattedXml().
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: [PB 5.22] Save XML and #LF$ in attribute

Post by freak »

The output is technically correct: The XML standard allows newlines within attributes. However as you found out, many XML parsers have a problem with this. This is why I changed the XML lib to encode newlines in attributes with &#xA in the 5.30 version. I did not make the change in 5.22 because it would be a change in the lib's behavior for something which is technically not even a bug.

So, if you can switch to 5.30 then this will fix your problem.
quidquid Latine dictum sit altum videtur
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Re: [PB 5.22] Save XML and #LF$ in attribute

Post by Le Soldat Inconnu »

Great :)

Thanks a lot
LSI
Post Reply