XMLdocument.XQL question

Hi there… I’m a newbie on xml and I have a big problem.

Scenario: I read an xmldocument(folderitem), and I’m searching for the value of a field… doing someting like this

nodes=MyXMLdoc.Xql("//ParentField/ChildField/AnotherChildField") For i As Integer = 0 To nodes.Length - 1 node = nodes.Item(i) k = XmlTextNode(node.FirstChild) MyRequest=k.Value Next

So my problem is that this code works nice but not all the times… With some XML files it works every time, with some XML files it never works. The mentioned field is present and has got a value! Today I spent a lot of hours to understand why… and (I think) the only difference between the working and not-working files is the endofline… Seems that the not-working files has unix carriage return (seen by textwrangler) and the working files has mac or windows cr…

In the debug I can see that the string value (property of xmldocument) read by my app, is in one line only (no end of lines)…

So I tried to parse a string instead a folderitem… with this code

[code]dim t As TextInputStream
dim s As String

t=TextInputStream.Open(xmlFile)
t.Encoding=Encodings.UTF8
s=t.ReadAll(Encodings.UTF8)
s=ReplaceAll(s,EndOfLine.UNIX,EndOfLine.Macintosh)

XMLDoc=new XmlDocument

Try
XMLDoc.LoadXml(s)
Catch e As XmlException
MsgBox(“This does not appear to be an XML file.”)
Return
End Try[/code]

But I have no results… that XML files containing values that I can’t read :frowning:

Anyone have ideas? Have I to provide any other information about my problem?

Thanks a lot in advance…

please see : https://forum.xojo.com/51275-xml-attributes/p1#p415603
this method will look in all nodes with xql to get all the values you need.
the method must be placed in a module and must be public.

Public Function nodeText(extends node as XmlNode, subNodeName as String = "") as String If subNodeName="" Then If node.FirstChild<>Nil And node.FirstChild IsA XmlTextNode Then Return node.FirstChild.Value Else Dim xql As New XmlNodeList xql=node.Xql(subNodeName+"/text()") If xql.Length=1 Then Return xql.Item(0).Value End If End If End Function

usage :

System.DebugLog("IP: " + xml.DocumentElement.nodeText("IP")) System.DebugLog("ErrCount: " + xml.DocumentElement.nodeText("ErrCount")) System.DebugLog("ResponseCount: " + xml.DocumentElement.nodeText("ResponseCount")) System.DebugLog("Done: " + xml.DocumentElement.nodeText("Done")) System.DebugLog("debug: " + xml.DocumentElement.nodeText("debug"))

[quote=422583:@Jean-Yves Pochez]please see : https://forum.xojo.com/51275-xml-attributes/p1#p415603
this method will look in all nodes with xql to get all the values you need.
the method must be placed in a module and must be public.
…[/quote]

So after te XQL request do I have to add “/text()”… but in my case I have an empty string in this way too…

The strange ting is that in the XML file, the data are present… I paste here a part of a real example of the xml

<DatiAnagrafici> <IdFiscaleIVA> <IdPaese>IT</IdPaese> <IdCodice>10101010101010</IdCodice> </IdFiscaleIVA> <CodiceFiscale>XYZSRG73C08B999X</CodiceFiscale> <Anagrafica> <Denominazione>NAME HERE</Denominazione> </Anagrafica> <RegimeFiscale>RF01</RegimeFiscale> </DatiAnagrafici>

I’m searching for “CodiceFiscale” field

I repeat: in many xml docs it all works, in other don’t :frowning:

[quote=422581:@Sergio Tamborini]Hi there… I’m a newbie on xml and I have a big problem.

Scenario: I read an xmldocument(folderitem), and I’m searching for the value of a field… doing someting like this

nodes=MyXMLdoc.Xql("//ParentField/ChildField/AnotherChildField") For i As Integer = 0 To nodes.Length - 1 node = nodes.Item(i) k = XmlTextNode(node.FirstChild) MyRequest=k.Value Next

So my problem is that this code works nice but not all the times… With some XML files it works every time, with some XML files it never works. The mentioned field is present and has got a value! Today I spent a lot of hours to understand why… and (I think) the only difference between the working and not-working files is the endofline… Seems that the not-working files has unix carriage return (seen by textwrangler) and the working files has mac or windows cr…

In the debug I can see that the string value (property of xmldocument) read by my app, is in one line only (no end of lines)…

So I tried to parse a string instead a folderitem… with this code

[code]dim t As TextInputStream
dim s As String

t=TextInputStream.Open(xmlFile)
t.Encoding=Encodings.UTF8
s=t.ReadAll(Encodings.UTF8)
s=ReplaceAll(s,EndOfLine.UNIX,EndOfLine.Macintosh)

XMLDoc=new XmlDocument

Try
XMLDoc.LoadXml(s)
Catch e As XmlException
MsgBox(“This does not appear to be an XML file.”)
Return
End Try[/code]

But I have no results… that XML files containing values that I can’t read :frowning:

Anyone have ideas? Have I to provide any other information about my problem?

Thanks a lot in advance…[/quote]
If that’s the case, have you tried calling ReplaceLineEndings on the XML string before trying to use XQL?

Sergio, you can use the extension method proposed.
Say that x is you xmlDocument, so your startNode is x.DocumentElement

Dim codiceFiscale as string=x.DocumentElement.nodeText("CodiceFiscale")

//but also:
dim identificativoPaese as string=x.DocumentElement.nodeText("IdFiscaleIVA/IdPaese")

//and as when you use attributes you get nothing (ie empty string) for a non existent node:
dim fakeValue as string=x.DocumentElement.nodeText("pippo")

obviously if your fragment is a specific node (Y as xmlNode) you have to write:

Dim codiceFiscale as string=Y.nodeText("CodiceFiscale")

and so on…

[quote=422712:@Antonio Rinaldi]Sergio, you can use the extension method proposed.
Say that x is you xmlDocument, so your startNode is x.DocumentElement
[/quote]
This is very clear to use, once I understand how to do :slight_smile:

But another problem I have (solved for the moment)…

The standard tree for the xml files I have to process, is an header like this

 <FatturaElettronicaHeader>

and sometimes I found

<FatturaElettronicaHeader xmlns="">

so using

myvar=trim(XMLDoc.DocumentElement.nodeText("FatturaElettronicaHeader/CessionarioCommittente/DatiAnagrafici/CodiceFiscale"))
returns and empty string.

For the moment I solved reading the xml as string with textinputstream and replace all xmlns="" string with and empty space… If anyone have a better suggestion…

Thanks to all for your precious help!