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?
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.
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
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).
Getting XML fragment as text using stringValue() method removes all tags from 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.
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)
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:
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
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 to 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.