Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 douglassophies · Jan 15, 2013 at 02:23 PM · xmlnodes

How to access embedded nodes without system.xml

After getting a bit of an answer from here: http://answers.unity3d.com/questions/378690/converting-from-systemxml-to-monoxml.html I decided to try and use the suggested TinyXML class to read my data. However, i cannot get to information that is stored deep within tags. For example, i want to get the content from the second of three "string" tags that are all stored inside a "dict" tag which is stored inside another "dict" tag. If it cannot be done with TinyXML then i am happy to change to any other suggestion.

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 CHPedersen · Jan 15, 2013 at 02:25 PM 0
Share

Out of curiosity: Why do you wish to avoid System.Xml in the first place? It's a very powerful X$$anonymous$$L library, and easily supports what you're trying to do.

avatar image Graham-Dunnett ♦♦ · Jan 15, 2013 at 03:41 PM 0
Share

@CHPedersen - but has lots of dependencies, so for simple reading of small xml files it's overkill.

avatar image douglassophies · Jan 15, 2013 at 03:50 PM 0
Share

As Graham pointed out its overkill. This particular job is for a 2D animation script, which i hope to be able to use on any platform and donate to others, so its worth getting it streamlined.

1 Reply

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

Answer by Graham-Dunnett · Jan 15, 2013 at 10:25 PM

Try this xmltest.cs. This kinda shows how to use mono.xml. I've not used this code in production, but it compiles, and dumps out the expected values to the console. You'll need to grab the mono.xml zip file linked to from the Unity website, and copy the files into your project.

 //c#
 using UnityEngine;
 using System.Collections;
 using Mono.Xml;
 using System.IO;
 
 public class xmltest : MonoBehaviour {
     
     class Handler : SmallXmlParser.IContentHandler {
         public void OnStartParsing (SmallXmlParser parser)
         { // start of file
         }
 
         public void OnEndParsing (SmallXmlParser parser)
         { // end of file
         }
 
         public void OnStartElement (string name, SmallXmlParser.IAttrList attrs)
         { // opening tag, with attributes
             Debug.Log ("Got <" + name + ">");
             foreach (string s in attrs.Names)
                 Debug.Log ("Attribute name: " + s);
             foreach (string t in attrs.Values)
                 Debug.Log ("Attribute value: " + t);
         }
 
         public void OnEndElement (string name)
         { // closing tag
         }
 
         public void OnChars (string s)
         { // string between open and closing tag
             Debug.Log (s);
         }
 
         public void OnIgnorableWhitespace (string s)
         {
         }
 
         public void OnProcessingInstruction (string name, string text)
         {
         }
     }
     
     void Start () {
         SmallXmlParser x = new SmallXmlParser();
         
         Handler h = new Handler();
         TextReader textReader = new StreamReader(Application.dataPath + "/test.xml");
         x.Parse(textReader, h);
     }
 }

I tested this code with the following in Assets/test.xml.

 <?xml version="1.0"?>
 <dict>
   <dict>
     <string jim="sheila">fred</string>
     <string>Douglas</string>
     <string>Another string</string>
   </dict>
 </dict>

Note that there is no way to say "get me the second string two dicts down". You'll need to count the number of calls into OnEndElement() when string name is equal to dict, and then look for the second call to OnEndElement() when string name is equal to string. If you know that your xml is clean, and the string you need is always the 4th tag, then just count 4 tags.

I am certain that you can achieve the same with TinyXML. I have never used that.

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 douglassophies · Jan 16, 2013 at 09:14 PM 0
Share

Thank you for the detailed help! That is a great example to get started with. I solved the problem of getting to the correct tags by using bool flags. I had a few other problems that i thought i would mention here for others. First, i was getting "This parser does not support document type." I manually modified my xml by taking out the top line which fixed it. This is only a temporary fix, but it meant i don't have to modify the mono document. However, my next issue came with an "Inconsistent accessibility error". Here i was forced to make one small change and swop "internal class SmallXmlParser" for "public class SmallXmlParser". If anyone wants to see my final solution then let me know. I will be posting the animation class up soon that uses it.

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.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

XML parser, how to get to 1 of many elements with the same name. 1 Answer

XML parsing and create UI 0 Answers

XML SelectNodes 0 Answers

Serializing and Deserializing from/to XML inside Resources folder 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges