Sunday, November 27, 2022

#1 2022-09-29 09:52:56 am

rayascott
Member
Registered: 2020-03-23
Posts: 17

XML element as text?

Anyone know if it's possible to coerce an XML element to text or data? I've not had any luck, and was thinking I'd have to use Foundation.

Also although XML file has a property called XML data, I've had absolutely no luck being able to reference it without a compiler revolt. Is it some mythical property that is named that way just to get in the way of the class of the same name?


Filed under: text, xml, coersion, XML element

Offline

 

#2 2022-09-29 10:42:33 am

robertfern
Member
Registered: 2011-11-29
Posts: 223

Re: XML element as text?

Where is this XML element?
Is it on the clipboard, or in a file, or is it coming from a website?

Give an example

Offline

 

#3 2022-09-30 01:57:34 am

rayascott
Member
Registered: 2020-03-23
Posts: 17

Re: XML element as text?

Its being read in via a XML file AppleScript object.

I'm not sure the data's origin is of significance. I'm not asking for massive work-arounds that involve a solution outside of the XML Suite in System Events.

I'm just looking to get an XML element object coerced to text so I can treat that part of the XML document as a text and not XML elements, as you have to once it's read in via XML file object creation.

Offline

 

#4 2022-09-30 09:06:28 am

robertfern
Member
Registered: 2011-11-29
Posts: 223

Re: XML element as text?

I found a previous thread about XML here

https://macscripter.net/viewtopic.php?p … 93#p190993

It would be nice to have a sample of the XML

Offline

 

#5 2022-09-30 09:13:08 am

rayascott
Member
Registered: 2020-03-23
Posts: 17

Re: XML element as text?

Sample XML is completely besides the point. All I want is for the last call to succeed:

set xmle to XML element 1 of ... -- the root element of some XML file
set mytext to xmle as text

Offline

 

#6 2022-09-30 09:18:55 am

robertfern
Member
Registered: 2011-11-29
Posts: 223

Re: XML element as text?

Here is a sample test script that works for me

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local xfile, xdata, xmlText, xmlspot, xElement

tell application "System Events"
   --set xdata to make new XML data with properties {text:xmlText}
   set xfile to XML file "Mac SSD:Users:robert:Downloads:Intel_USB_3.0_xHC_Driver_Skylake_MR6_PV_4.0.6.60:mup.xml"
   set xElement to XML element 1 of xfile
   get name of xElement
   get value of xElement
end tell

Offline

 

#7 2022-09-30 09:22:52 am

rayascott
Member
Registered: 2020-03-23
Posts: 17

Re: XML element as text?

I don't want the name and value of the element. I want the text, the raw XML...

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Offline

 

#8 2022-09-30 09:24:46 am

robertfern
Member
Registered: 2011-11-29
Posts: 223

Re: XML element as text?

Then just read the XML file as a text file

Offline

 

#9 2022-09-30 09:33:03 am

rayascott
Member
Registered: 2020-03-23
Posts: 17

Re: XML element as text?

This exchange is only slightly less painful than root canal. ;-)

If I wanted to read the entire file in as text, I would have simply done that, hence my original question still stands. How do you coerce a single XML element to text (or data).

Offline

 

#10 2022-09-30 09:38:39 am

robertfern
Member
Registered: 2011-11-29
Posts: 223

Re: XML element as text?

The painful part is the not giving examples of the XML and what part you want out of it.
I need something to work with.
But you refuse to give it.

Offline

 

#11 2022-10-04 07:12:55 am

technomorph
Member
Registered: 2017-12-14
Posts: 302

Re: XML element as text?

Ask for the NSXMLElement’s stringValue

Look into using NSXMLDocument

have a look at:

https://macscripter.net/viewtopic.php?id=49203
https://macscripter.net/viewtopic.php?id=49139

Last edited by technomorph (2022-10-06 07:34:52 pm)

Offline

 

#12 2022-11-17 01:36:45 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2583

Re: XML element as text?

rayascott wrote:

I don't want the name and value of the element. I want the text, the raw XML...

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>



Getting XML fragment as text using stringValue() method removes all tags from <note> NSXMLElement's HTML content. Therefore it will be useless if applied to it in this case.

Getting NSXMLElement as raw data is possible, only I don't know how the OP is going to use the result further.

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//note" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElement to theMatches's firstObject()
set rawData to «class seld» of (noteElement as record)

Last edited by KniazidisR (2022-11-20 02:30:52 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#13 2022-11-20 02:25:45 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2583

Re: XML element as text?

Today I had more free time and found a way to get nested XML content (with tags preserved). So I will delete the second script in my previous post as less successful.

Solution using XMLString() method from NSXMLNode class documentation:

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//note" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElement to theMatches's firstObject()

set nameLength to length of (noteElement's |name|() as text)
(noteElement's XMLString() as text)'s text (nameLength + 3) thru (-4 - nameLength)
-- OR, if you need include <note> and </note> tags in the result, then simply:
----- (noteElement's XMLString()) as text

Last edited by KniazidisR (2022-11-20 02:41:13 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#14 2022-11-22 04:17:32 am

technomorph
Member
Registered: 2017-12-14
Posts: 302

Re: XML element as text?

Skip the trimming get the nodes

value()
Then get the stringValue()

Also play around with innerText()

Offline

 

#15 2022-11-22 05:23:01 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2583

Re: XML element as text?

technomorph wrote:

Skip the trimming get the nodes

value()
Then get the stringValue()

Also play around with innerText()


What are you speaking about? The script in post #13 gives the inner HTML content.

As for the value() and innerText() methods, they doesn't exist in the NSXMLElement or NSNode documentation, as I see. I don't seem to understand anything from your last post. My code is not JsObjC or JavaScript or JXA but AsObjC...


Re-reading post #5 and the OP's code snippet again, I'm convinced that they does convert the XML file content to plain text, and then wants to parse the text from <note> to </note> using shell commands (like grep, sed, tr...) or using text item delimiters. And post #1 is not about parsing text, but about getting the content of an XML element (known as innerHTML), I have no words!

The topic interested me from a post #1 point of view - obtaining inner HTML content directly from an XML element.

Here on the site it has already been shown thousands of times how to cut a desired section of text using shell commands (like grep, sed, tr ...) or using text item delimiters. Therefore, I leave this topic on this point.

Last edited by KniazidisR (2022-11-22 06:35:33 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#16 2022-11-22 08:23:32 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1083

Re: XML element as text?

Or maybe something like this.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//note" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElements to theMatches's allObjects()
set elements to noteElements's firstObject()

repeat with i from 1 to (elements's childCount) as integer
   log (elements's childAtIndex:(i - 1))'s objectValue() as string
end repeat

Last edited by Fredrik71 (2022-11-22 08:23:55 am)


Node-RED makes it easy to automate IoT

Offline

 

#17 2022-11-22 08:38:58 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1083

Re: XML element as text?

Here is other that use name() and objectValue()

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//note" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElements to theMatches's allObjects()
set elements to noteElements's firstObject()

repeat with i from 1 to (elements's childCount) as integer
   set {theName, theValue} to {(elements's childAtIndex:(i - 1))'s |name|() as string, (elements's childAtIndex:(i - 1))'s objectValue() as string}
   log {theName, theValue}
end repeat


Node-RED makes it easy to automate IoT

Offline

 

#18 2022-11-22 09:32:45 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1083

Re: XML element as text?

Lets say you like to return the from tag in XML with @KniazidisR code.
We do little change in nodesForXPath method to use //from
The return will use description method to be able to return the contents.

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//from" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElements to theMatches's allObjects()
set elements to noteElements's firstObject()
return elements's |description|() as string


Node-RED makes it easy to automate IoT

Offline

 

#19 2022-11-22 09:41:29 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1083

Re: XML element as text?

And here we make AS record from XML

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//note" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElements to theMatches's allObjects()
set theElements to noteElements's firstObject()
set theDict to current application's NSMutableDictionary's dictionary()
repeat with i from 1 to (theElements's childCount) as integer
   set {theName, theValue} to {(theElements's childAtIndex:(i - 1))'s |name|() as string, (theElements's childAtIndex:(i - 1))'s objectValue() as string}
   (theDict's setObject:theValue forKey:theName)
end repeat
return theDict as record

Last edited by Fredrik71 (2022-11-22 09:43:15 am)


Node-RED makes it easy to automate IoT

Offline

 

#20 2022-11-22 10:09:42 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1083

Re: XML element as text?

rayascott wrote:

I don't want the name and value of the element. I want the text, the raw XML...

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>



You could do that with this code.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BrightSignUserVariables>
<BrightSignVar name=\"CurrentLetter\">W</BrightSignVar>
<dict>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</dict>
<BrightSignVar name=\"CurrentUserName\">&Q&W&Q&W&Q&W&Q&W</BrightSignVar>
</BrightSignUserVariables>"


set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithXMLString:theXML options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)

set {theMatches, theError} to (theXMLDoc's nodesForXPath:"//note" |error|:(reference))
if theMatches = missing value then error (theError's localizedDescription() as text)
set noteElements to theMatches's allObjects()
set elements to noteElements's firstObject()
return elements's |description|() as string


Node-RED makes it easy to automate IoT

Offline

 

#21 2022-11-24 01:16:20 am

technomorph
Member
Registered: 2017-12-14
Posts: 302

Re: XML element as text?

NSXMLNode doc https://developer.apple.com/documentati … guage=objc

Has
objectValue documention right there

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)