Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Story Specialist · Mar 12, 2010 at 12:24 PM · javascriptxmlparser

How can I find specific parts of an XML and feed them into dialogue?

I've tried googling and searching Unity Answers/Forums, but all I come up with are things to load entire XML documents. What I would like to do is make a Javascript (preferably Java) that will allow me to read specific dialogue lines from an XML file.

I got this, which seems to be similar to what I need: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=312&page=1 (also found an XML parser, but that loads entire XMLs as well[?]: http://forum.unity3d.com/viewtopic.php?t=40708&highlight=xml+parse)

But I have no idea how I would turn this into Javascript, I'm really a beginner.

So, in short, I want a script that can read, for instance, this XML:

<scenes>
  <scene1>
   <actor>Bob</actor>
   <line1>"Oh, hi thare!"</line1>
   <line2>"This is my second line."</line2>
  </scene1>
  <scene2>
   <actor>Bill</actor>
   <line1>"I'm another actor!"</line1>
  </scene2>
</scenes>

Naturally I would want to load that function from another script. I hope you can help me out! If there is more information needed, I'll be happy to supply it.

Comment
Add comment
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

2 Replies

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

Answer by Flimgoblin · Mar 12, 2010 at 09:35 PM

If you use the parser that you linked to you can do something like:

public var dialogueXMLFile:TextAsset; public var dialogue:XMLNode;

function Start(){ var parser=new XMLParser(); dialogue=parser.Parse(dialogueXMLFile.text); }

You can then access your dialogue as:

Debug.Log(dialogue.GetValue("scene>0>actor>0>line>0>_text"));
Debug.Log(dialogue.GetValue("scene>0>actor>0>line>1>_text"));

(though the arrays start from 0 rather than 1 unlike your ids in your XML document)

If you want to loop through the dialogue you can do:

for(var n:XMLNode in dialogue.GetNodeList("scene>0>actor>0>line")){
         Debug.Log(n.GetValue("_text"));
}

Complete script I'm testing with:

public var dialogueXMLFile:TextAsset; public var dialogue:XMLNode;

function Start(){ var parser=new XMLParser(); dialogue=parser.Parse(dialogueXMLFile.text); Debug.Log(dialogue.GetValue("scene>0>actor>0>line>0>_text")); Debug.Log(dialogue.GetValue("scene>0>actor>0>line>1>_text"));

for(var n:XMLNode in dialogue.GetNodeList("scene>0>actor>0>line")){ Debug.Log(n.GetValue("_text")); } }

Comment
Add comment · Show 7 · 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 Story Specialist · Mar 14, 2010 at 01:31 PM 0
Share

Okay, this is probably a really stupid question, but when I do that, I get:

"Assets/Scripts/Intro1_GUI.js(34,8): BCE0044: expecting function, found 'TextAsset'."

"Assets/Scripts/Intro1_GUI.js(35,8): BCE0044: expecting function, found 'X$$anonymous$$LNode'."

It is still Javascript you're using, right?

I just added the file from the X$$anonymous$$L Parser to my Scripts folder in my assets, am I leaving something out?

avatar image Story Specialist · Mar 14, 2010 at 01:40 PM 0
Share

Or should I copy/paste the content of those scripts into my own script?

avatar image Flimgoblin · Mar 15, 2010 at 12:00 PM 0
Share

Doh, sorry I'd just lifted the TextAsset stuff from DerWoSaDo's example which was C# syntax.

I've updated the script to be JavaScript all the way through, unable to test at the moment but can take another look tonight if it's still causing you grief.

avatar image Story Specialist · Mar 15, 2010 at 12:10 PM 0
Share

Well, I kinda of figured that out, and did make it Javascript, but I still get: Assets/Scripts/Intro1_GUI.js(34,8): BCE0044: expecting function, found 'dialogueX$$anonymous$$LFile'.

avatar image Flimgoblin · Mar 15, 2010 at 07:20 PM 0
Share

Bleh, that's what ye get when making up code without testing it :) Working version posted, this one's tested :P sorry about that :)

Show more comments
avatar image
1

Answer by DerWoDaSo · Mar 12, 2010 at 12:50 PM

Getting the dialogue lines from the XML file, every time you need them can be quiet slow. But you can parse the XML file at the beginning and store the dialogue lines in an array for example. I would also change the XML structure a little bit... but you structure will do it as well. :)

<scene nr="1">
  <actor name="Bob">
   <line id="1">"Oh, hi thare!"</line>
   <line id="2">"This is my second line."</line>
  </actor>
</scene>
<scene nr=2>
  <actor name="Bill">
   <line id="1">"I'm another actor!"</line>
  </actor>
</scene>

And here an idea for the script (sorry, it's in C#):

public TextAsset dialogueXMLFile;

private void parseXML() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(dialogueXMLFile.text); XmlNodeList NodeList = xmlDoc.GetElementsByTagName("scene");

foreach (XmlNode sceneNode in NodeList) { //Get Scene Number int sceneNr = sceneNode.Attributes["nr"].Value;

    //get actors
    foreach (XmlNode actorNode in sceneNode.ChildNodes)
    {
        //Get Actor name
        string actorName = actorNode.Attributes["name"].Value;

        //get lines and store the data in an array or what ever...
    }

}

Comment
Add comment · Show 2 · 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 Story Specialist · Mar 12, 2010 at 01:12 PM 0
Share

Okay, I think I get that. How would I go about calling the lines? would I go xmlDoc.Bob.1 for instance?

avatar image Story Specialist · Mar 15, 2010 at 10:17 AM 0
Share

I tried using your script, but I get all kinds of errors, like this one: Assets/Scripts/NewBehaviourScript.cs(20,17): error CS0246: The type or namespace name `XmlDocument' could not be found. Are you missing a using directive or an assembly reference?

All related to Xml.

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Using JSON to get tree data into JavaScript objects 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to toggle a key for a car to go forward or backward? 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