Author Topic: XmlElement.GetAttribute  (Read 1097 times)

sasha19323

  • Jr. Member
  • **
  • Posts: 99
  • Karma: +25/-24
    • View Profile
XmlElement.GetAttribute
« on: December 01, 2012, 12:34:19 pm »
Hi, guys! I need to get attributes from my xml file. Can u show me it with example?

Kind regards

Thijn

  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: XmlElement.GetAttribute
« Reply #1 on: December 02, 2012, 11:27:41 am »
This is a part of my script, hopefully you can get the part you need out of it.
Code: [Select]
local XmlDoc = XmlDocument( "Maps/"   Map );
if ( XmlDoc )
{
XmlDoc.LoadFile();
local xmlRoot = XmlDoc.FirstChild( "Map" );
local xmlInfoRoot = xmlRoot.FirstChild( "Info" );
local xmlObjectsRoot = xmlRoot.FirstChild( "Objects" );
local Author = xmlInfoRoot.GetAttribute( "Author" );
local Date = xmlInfoRoot.GetAttribute( "Date" );
local Name = xmlInfoRoot.GetAttribute( "Name" );
local plr = FindPlayer( 0 );
MessagePlayer( "Loading Map "   Name, plr, Colour(0, 183, 235) );
MessagePlayer( "   - Made by "   Author, plr, Colour(0, 183, 235) );
MessagePlayer( "     on "   Date, plr, Colour(0, 183, 235) );
local id = 0, a = true, node = xmlObjectsRoot.FirstChild( "Object" ), LastOb;
while( node )
{
local vPos = Vector( node.GetAttribute( "x" ).tofloat(), node.GetAttribute( "y" ).tofloat(), node.GetAttribute( "z" ).tofloat() );
local vAngle = Vector( node.GetAttribute( "RotX" ).tofloat(), node.GetAttribute( "RotY" ).tofloat(), node.GetAttribute( "RotZ" ).tofloat() );
LastOb = CreateObject( node.GetAttribute( "model" ).tointeger(), vPos );
LastOb.Angle = vAngle;

node = node.NextSibling( "Object" );
}
MessagePlayer( "Finished loading map.", plr, Colour( 0, 255, 0 ) );
CallClientFunc( FindPlayer( 0 ), "ObjectEdit/cmain.nut", "ToggleLoadWindow" );
}

For the XML File:
Code: [Select]
<Map>
    <Info Name="Test" Author="[VU]Thijn" Date="14:47 8-0-2012" />
    <Objects>
        <Object model="1378" x="1305.57" y="-100.05" z="16.3662" RotX="0" RotY="0" RotZ="0" />
<Object model="1378" x="1308.57" y="-100.05" z="18.3662" RotX="0" RotY="0" RotZ="0" />
    </Objects>
</Map>

sasha19323

  • Jr. Member
  • **
  • Posts: 99
  • Karma: +25/-24
    • View Profile
Re: XmlElement.GetAttribute
« Reply #2 on: December 02, 2012, 02:04:59 pm »
This is a part of my script, hopefully you can get the part you need out of it.
Code: [Select]
local XmlDoc = XmlDocument( "Maps/"   Map );
if ( XmlDoc )
{
XmlDoc.LoadFile();
local xmlRoot = XmlDoc.FirstChild( "Map" );
local xmlInfoRoot = xmlRoot.FirstChild( "Info" );
local xmlObjectsRoot = xmlRoot.FirstChild( "Objects" );
local Author = xmlInfoRoot.GetAttribute( "Author" );
local Date = xmlInfoRoot.GetAttribute( "Date" );
local Name = xmlInfoRoot.GetAttribute( "Name" );
local plr = FindPlayer( 0 );
MessagePlayer( "Loading Map "   Name, plr, Colour(0, 183, 235) );
MessagePlayer( "   - Made by "   Author, plr, Colour(0, 183, 235) );
MessagePlayer( "     on "   Date, plr, Colour(0, 183, 235) );
local id = 0, a = true, node = xmlObjectsRoot.FirstChild( "Object" ), LastOb;
while( node )
{
local vPos = Vector( node.GetAttribute( "x" ).tofloat(), node.GetAttribute( "y" ).tofloat(), node.GetAttribute( "z" ).tofloat() );
local vAngle = Vector( node.GetAttribute( "RotX" ).tofloat(), node.GetAttribute( "RotY" ).tofloat(), node.GetAttribute( "RotZ" ).tofloat() );
LastOb = CreateObject( node.GetAttribute( "model" ).tointeger(), vPos );
LastOb.Angle = vAngle;

node = node.NextSibling( "Object" );
}
MessagePlayer( "Finished loading map.", plr, Colour( 0, 255, 0 ) );
CallClientFunc( FindPlayer( 0 ), "ObjectEdit/cmain.nut", "ToggleLoadWindow" );
}

For the XML File:
Code: [Select]
<Map>
    <Info Name="Test" Author="[VU]Thijn" Date="14:47 8-0-2012" />
    <Objects>
        <Object model="1378" x="1305.57" y="-100.05" z="16.3662" RotX="0" RotY="0" RotZ="0" />
<Object model="1378" x="1308.57" y="-100.05" z="18.3662" RotX="0" RotY="0" RotZ="0" />
    </Objects>
</Map>
Thanks so much! You helped me!

 

© Liberty Unleashed Team.