Link to home
Start Free TrialLog in
Avatar of Guff_
Guff_

asked on

XML load without encoding possible?

Hi,

I have 3 xml files

xml1.xml :

<names>
   <name>Patrick</name>
</names>

xml2.xml :

<names>
   <name>Pätrick</name>
</names>

xml3.xml :

<?xml version="1.0" encoding="iso-8859-1" ?>
<names>
   <name>Pätrick</name>
</names>



now i want to load the xml.
I use :

set xml = Server.CreateObject("MSXML2.DOMDocument")
xml.async = false
xml.load("xml#.xml")
if xml.parseError.errorcode<>0 then Response.Write "Error" END IF
set xml = nothing

xml 1 and 3 don't give an Error
xml 2 does (due encoding probs)

now it comes :
it is possible to load xml 2 without Error WITHOUT changing the xml file itself ????????
Avatar of sybe
sybe

<names>
   <name>P&#228;trick</name>
</names>
Avatar of Guff_

ASKER

I can't change xml2 so thats not an option
There is an error because it isn't valid XML.

If you get it from somewhere, then you should ask them to send valid XML, not a string that looks like it is XML, but isn't.

The other option is to change to XML you get into something that is valid XML. I don't think that there is any other option.
Avatar of Guff_

ASKER

Option 1 : ask for valid xml
This not possible :(

Option 2 : convert invalid xml to valid xml
Ok sounds nice. Is there a simple way to do that?
What is the parseError ? I managed to load the doc ok.
Avatar of Guff_

ASKER

parseError:
An invalid character was found in text content.

I read somewhere that i could be a server "problem".
Well, I loaded it as a string using the following code and it parsed ok:

    <%
        sXml = "<names><name>Pätrick</name></names>"
        Set oDom = Server.CreateObject("MSXML2.DOMDocument.4.0")

        bOK = oDom.loadXML(sXml)

        If Not bOK Then
            Response.Write oDom.parseError.reason
        Else
            Response.Write oDom.selectSingleNode("/names/name").Text
        End If

        Set oDom = Nothing
    %>
Avatar of Guff_

ASKER

On that exact code i get:
Server.CreateObject Failed

I try without ".4.0" and i get Pätrick .. huh...?

But stored in an xml file on the server i get:
An invalid character was found in text content. ?
Hmm, why not read the file into a string and then load it? Would that help?
Avatar of Guff_

ASKER

@sybe
seems logical...
but how do i read the xml into a string?
sorry i'm a bit a n00b
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Guff_

ASKER

Tnx!

The solution was to do a binary2string conversion and then load it into a string:

Set xmlhttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
xmlhttp.Open "GET", urlXML, False
xmlhttp.send

' XML verwerken
sXml = "<?xml version='1.0' encoding='iso-8859-1'?>" & SimpleBinaryToString(xmlhttp.responseBody)
Set oDom = Server.CreateObject("MSXML2.DOMDocument")
bOK = oDom.loadXML(sXml)