• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by crazyKnight · May 22, 2012 at 05:03 AM · xmlparsing

Retrive value from xml

Hi all,

i want to retrive some values from a XML,here is the XML

 <?xml version="1.0"?>
 <transforms>
   <Happy>
     <FacePosition>
       <x>7</x>
       <y>8</y>
       <z>9</z>
     </FacePosition>
     <FaceRotation>
       <x>1</x>
       <y>2</y>
       <z>3</z>
     </FaceRotation>
     <EyeRotation>
       <x>4</x>
       <y>5</y>
       <z>6</z>
     </EyeRotation>
   </Happy>
   <nervous>
     <FacePosition>
       <x>1</x>
       <y>2</y>
       <z>3</z>
     </FacePosition>
     <FaceRotation>
       <x>4</x>
       <y>5</y>
       <z>6</z>
     </FaceRotation>
     <EyeRotation>
       <x>7</x>
       <y>8</y>
       <z>9</z>
     </EyeRotation>
   </nervous>
 </transforms>


Here's the method to retrive the values

 public static Vector3 LoadFromXml(string elementName,string typename)
     {
         float X = 0;
         float Y = 0;
         float Z = 0;
         
         string filepath = Application.dataPath + @"/Resources/Data.xml";
         //string filepath = Resources.Load("Data");
         
         XmlDocument xmlDoc = new XmlDocument(); 
         
         if(File.Exists (filepath))
         {
             xmlDoc.Load(filepath); 
                 
             XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
         
             foreach (XmlNode transformInfo in transformList)
             {
                 XmlNodeList transformcontent = transformInfo.ChildNodes;
                 
                 foreach (XmlNode transformItens in transformcontent) 
                 {
                     if(transformItens.Name == "x")
                     {
                             X = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
                     }
                     if(transformItens.Name == "y")
                     {
                              Y = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
                     }
                     if(transformItens.Name == "z")
                     {
                             Z = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
                     }
                 }
             }
         
         }
         return new Vector3(X,Y,Z);;
     }


here element name is the name of the state which could be happy or nervous and type name is the name of the sub element facerotation or faceposition or eye rotation.i want to retrive coordinates for a specific state and its subelement.

please help me out i am completely new to XML

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image crazyKnight · May 22, 2012 at 05:14 AM 0
Share

guys please help ...

avatar image flamy · May 22, 2012 at 05:51 AM 0
Share

wat is the problem error? null ref. or something else?!?! you just posted the code and didn't say way the problem is!

avatar image crazyKnight · May 22, 2012 at 06:16 AM 0
Share

@flamy the problem was i was not able to retrive the specific vallues as i am completely new to xml,i have solved the problem and will post the solution.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by crazyKnight · May 22, 2012 at 06:17 AM

solved myself

heres the solution

 public static Vector3 LoadFromXml(string elementName,string typename,int stage)
 {
     float X = 0;
     float Y = 0;
     float Z = 0;
     string p1 = "x";
     string p2 = "y";
     string p3 = "z";

     string filepath = Application.dataPath + @"/Resources/Data.xml";
     //string filepath = Resources.Load("Data");
     
     XmlDocument xmlDoc = new XmlDocument(); 
     
     if(File.Exists (filepath))
     {
         xmlDoc.Load(filepath); 
             
         XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
     
         foreach (XmlNode transformInfo in transformList)
         {
             XmlNodeList transformcontent = transformInfo.ChildNodes;
             
             foreach (XmlNode transformItens in transformcontent) 
             {
                 if(transformItens.Name == typename)
                 {
                     XmlNodeList newtransformList = transformItens.ChildNodes;
                         
                         foreach (XmlNode newtransformItens in newtransformList)
                         {
                                 if(newtransformItens.Name == "x")
                                 {
                                         X = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
                                 }
                                 if(newtransformItens.Name == "y")
                                 {
                                          Y = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
                                 }
                                 if(newtransformItens.Name == "z")
                                 {
                                         Z = float.Parse(newtransformItens.InnerText); // convert the strings to float and apply to the Y variable.
                                 }
                         } 
                 }
             }
         }
     
     }
     return new Vector3(X,Y,Z);;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacobtw · May 27, 2013 at 02:01 PM 0
Share

This is exactly what I'm looking for I can't get it to work though:(

Is this a working example or did you leave something out?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Parse xml with unknown fields 1 Answer

Handle HTML response 0 Answers

Parseing Varible names from strings 1 Answer

Parsing an XML file periodically 2 Answers

How would my WEB APP knows which username is accessing it? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges