I have an XML file containing elements named “asset” that each represent a file like the one listed below.
<asset id="r2" name="00018M" uid="DF36C3050CEDE81F705DC99FCF0E1487">
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/00018M.MXF">
</media-rep>
<metadata>
<md key="com.apple.rawToLogConversion" value="0"/>
<md key="com.apple.kMDItemProfileName" value="SD (6-1-6)"/>
</metadata>
</asset>
I can get the file’s attributes such as “name” and “uid” from the “assets” element easily. But I can’t figure out how to get the corresponding “src” attribute from the “media-rep” element.
Any help appreciated.
media-rep is a Child of asset
For constant kXML
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<asset id="r2" name="00018M" uid="DF36C3050CEDE81F705DC99FCF0E1487">
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/00018M.MXF">
</media-rep>
<metadata>
<md key="com.apple.rawToLogConversion" value="0"/>
<md key="com.apple.kMDItemProfileName" value="SD (6-1-6)"/>
</metadata>
</asset> </catalog>
get “src”
[code]Sub Action()
Var xml As New XmlDocument(kXML)
Var root As XmlNode
root = xml.DocumentElement
Var asset As XmlNode
Var media As XmlNode
asset = root.FirstChild
For idx As Integer = 0 To asset.ChildCount - 1
media = asset.Child(idx)
Var src As String = media.GetAttribute("src")
Break
Next
End Sub[/code]
XML + XQL is probably much simpler
// find all media-reps that are contained by an asset
// and get their src attribute
// // is the descendent operator
// see http://www.ibiblio.org/xql/xql-proposal.html
Dim srcs As XmlNodeList = docNode.XQL("//asset/media-rep/@src")
For i As Integer = 0 To srcs.Length - 1
Dim currentSrc As XmlNode = srcs.Item(i)
System.debuglog currentSrc.Value
Next
Thank you, Oliver and Norman for your solutions. Both work fine for me and I am happy to have learned two ways of doing this.
Many thanks.
@Norman Palardy
Can you take the XML +XQL method one step further and find just the “src” item that has the value of “x”? Or is it necessary to iterate the srcs list?
with XQL you can find node with a bunch of criteria
https://www.w3schools.com/xml/xpath_intro.asp
Start a new project
In Windows1 add this String constant to the window
Private Const kXML as String = <asset id="r2" name="00018M" uid="DF36C3050CEDE81F705DC99FCF0E1487">
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/00018M.MXF">
</media-rep>
<metadata>
<md key="com.apple.rawToLogConversion" value="0"/>
<md key="com.apple.kMDItemProfileName" value="SD (6-1-6)"/>
</metadata>
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="00118M.MXF">
</media-rep>
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/20018M.MXF">
</media-rep>
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/03018M.MXF">
</media-rep>
<media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/00418M.MXF">
</media-rep>
</asset>
this code in the open event finds a specific src value
Sub Open() Handles Open
// find all media-reps that are contained by an asset
// and get their src attribute
// // is the descendent operator
// see http://www.ibiblio.org/xql/xql-proposal.html
Dim xmlDoc As New XmlDocument(kXML)
Dim docNode As XmlNode = xmlDoc.DocumentElement
Dim srcValueToFind As String = "file:///Volumes/Storage2/20018M.MXF"
Dim srcs As XmlNodeList = docNode.XQL("//media-rep[@src='" + srcValueToFind + "']")
For i As Integer = 0 To srcs.Length - 1
Dim currentSrc As XmlNode = srcs.Item(i)
System.debuglog currentSrc.ToString
Next
End Sub
Thank you, Norm, for the example and link. Im beginning to get it!