Parsing XML

I’m finding it quite hard to find any really info on Xojo’s XML system and what I have found isn’t migrating that well to what I already know, so if all else fails … ask!

I have a XML file that I need to get selected names and values
This is the file:
https://dl.dropboxusercontent.com/u/26539256/temp.xml

I need to be able to list the Object Camera names but also Parameters and return the values
This is the C# version, for collecting the camera names, its commented from when I did this version
The XML file is created by a 3D Render engine

[code] string path = iptPath + slashes + “temp.xml”;
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment;
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(path))
{
while ((reader.Read()))
{
if ((reader.NodeType == System.Xml.XmlNodeType.Element))
{

                            if ((reader.Name == "Object"))
                            {
                                //Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
                                string Identifier = reader.GetAttribute("Identifier");
                                //"./Cameras/MyCamera"
                                string Parameter = reader.GetAttribute("Parameter");

                                string Name = reader.GetAttribute("Name");

                                string Label = reader.GetAttribute("Label");
                                //"Standard Camera"

                                //"MyCamera"
                                string Type = reader.GetAttribute("Type");
                                //"Camera"
                                string Value2 = reader.GetAttribute("Value");

                                string wholeString = Name;

                                //Adding the string to cameralist
                                if ((wholeString.Length > 0) & Type == "Camera")
                                {
                                    cameralist.Items.Add(wholeString);
                                }
                            } [/code]

thanks for your time
Nige

dim f as folderItem=<folderitem to your xml file>
dim xDoc as new xmlDocument
try
  xDoc.load(f)
catch
  xDoc=nil //an error occurred
end try

dim names() as String
if xDoc<>nil then
  dim xq as XmlNodeList
  xq=xDoc.DocumentElement.Xql("Object/Object[@Type='Camera']")
  dim i,n as integer
  n=xq.Length-1
  for i=0 to n
    names.Append xq.Item(i).GetAttribute("Name")
  next
end if 

Thank you Antonio for the very quick reply and it works perfectly :slight_smile:
thanks again the help its really appreciated

I’m still having little problem getting parameter settings,
going by what Antonio said, how do I get the value in parameter “Focal Length (mm)” or any other names?

<Object Identifier="./Cameras/Camera" Label="Standard Camera" Name="Camera" Type="Camera"> <Parameter Name="Focal Length (mm)" Type="Real" Value="35"/> <Parameter Name="Film Height (mm)" Type="Real" Value="24"/> <Parameter Name="Shift X (mm)" Type="Real" Value="0"/> <Parameter Name="Shift Y (mm)" Type="Real" Value="0"/> <Parameter Name="Resolution" Type="String" Value="500x500"/> <Parameter Name="Frame" Type="Transform" Value="1 0 0 0.00364056 0 -2.05821e-08 1 -5.84642 0 -1 -2.05821e-08 1.49912 "/> <Parameter Name="Focus Distance" Type="Real" Value="5.79188"/> <Parameter Name="Shutter Speed" Type="Real" Value="125"/> <Parameter Name="f-number" Type="Real" Value="8"/> <Parameter Name="Depth of Field" Type="Real" Value="0.2"/> <Parameter Name="Blades" Type="Integer" Value="6"/> <Parameter Name="Diaphragm" Type="String" Value="Circular"/> <Parameter Name="Projection" Type="String" Value="Perspective"/> <Parameter Name="Auto-Focus" Type="Boolean" Value="0"/> <Parameter Name="DOF Lock" Type="Boolean" Value="0"/> <Parameter Name="Current View" Type="Boolean" Value="0"/> <Parameter Name="Locked" Type="Boolean" Value="1"/> <Parameter Name="Roll Lock" Type="Boolean" Value="0"/> <Parameter Name="Upwards" Type="String" Value="0 0 1"/> <Parameter Name="Region" Type="String" Value=""/> <Parameter Name="Render Region" Type="Boolean" Value="0"/> <Parameter Name="Interface Appearance" Type="String" Value=""/> <Parameter Name="Z-Clipping Near" Type="Boolean" Value="0"/> <Parameter Name="Z-Clipping Far" Type="Boolean" Value="0"/> <Parameter Name="Z-Clipping Near Distance" Type="Real" Value="1"/> <Parameter Name="Z-Clipping Far Distance" Type="Real" Value="1000"/> </Object>

I’ve been trying most of the week and I just don’t seem to get it
thanks
Nige

with the previous example, you’ve got the camera name array with the list of nodes

for every node you can query (xql) for sub nodes

therefore depends on how you want to do the search for the parameter
if you want to do it in the flow of the list:

//in the loop of previous example
names.Append xq.Item(i).GetAttribute("Name")
dim xqp as xmlNodeList
xqp=xq.item(i).xql("Parameter[@Name='Focal Length (mm)']")
if xqp.length>0 then myFocalLength=xqp.item(0).getAttribute("Value")

if you want to do it from a name of a Camera and for a generic parameter:

function paramValue(xDoc as xmlDocument,cameraName as string,paramName as string) as string
dim xq as xmlNodeList
xq=xDoc.documentElement.xql("Object/Object[@Type='Camera' and @Name='"+cameraName+"']/Parameter[@Name='"+ paramName+"']")
if xq.length>0 then return xq.getAttribute("Value")
end function

This function will return the parameter string Value, note that it will be empty either for a node that does not exist or for a value intentionally blank

Thank you again for the speedy reply, the first example was just what I needed

I just have to figure out how to do my socket client now :slight_smile:

Sorry for being a pain but if I have a node name with forward slashes ie

@Name='./Render/Channels/Normal'

the line appears to get ignored
if its a name without slashes it works fine
I just had a look around and I can’t find anything related to this problem, :s

[quote=126152:@nige cope]Sorry for being a pain but if I have a node name with forward slashes ie

@Name='./Render/Channels/Normal'

the line appears to get ignored
if its a name without slashes it works fine
I just had a look around and I can’t find anything thing related to this problem, :s[/quote]

A similar issue has been discussed here :
http://stackoverflow.com/questions/539143/xml-entity-for

Strange, I’ve tried against your document and it works
Note that that parameter is not a “Camera” object is a “Scene” object

xq=xDoc.documentElement.xql(“Object/Parameter[@Name=’”+ paramName+"’]")

Yes I just realised what I was doing wrong, with C# and net I could read the parameters “on the fly” without having them in an Object group… I wasn’t looking inside the group basically

I’m on a quest to get my little Batch rendering program cross platform and I think my brain’s fried lol
originally it was written VB.Net, so I moved it to C# but I loath Mono, also QT, the problem really is I know nothing about Mac and the QT attempts was a disaster, Xojo seems the most painless way to go but yet another learning curve

But, yes it was my mistake, I now have it working :slight_smile: