Where is this XML element?
Is it on the clipboard, or in a file, or is it coming from a website?
Give an example
Where is this XML element?
Is it on the clipboard, or in a file, or is it coming from a website?
Give an example
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.
I found a previous thread about XML here
https://macscripter.net/viewtopic.php?pid=190993#p190993
It would be nice to have a sample of the XML
Sample XML is completely besides the point. All I want is for the last call to succeed:
[format]
set xmle to XML element 1 of … – the root element of some XML file
set mytext to xmle as text
[/format]
Here is a sample test script that works for me
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
I don’t want the name and value of the element. I want the text, the raw XML…
Tove Jani Reminder Don't forget me this weekend!Then just read the XML file as a text file
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).
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.
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
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
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 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.
Or maybe something like this.
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
Here is other that use name() and objectValue()
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
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.
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
And here we make AS record from XML
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
You could do that with this code.
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
NSXMLNode doc https://developer.apple.com/documentation/foundation/nsxmlnode?language=objc
Has
objectValue documention right there