- Home /
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.
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")); } }
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?
Or should I copy/paste the content of those scripts into my own script?
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.
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'.
Bleh, that's what ye get when making up code without testing it :) Working version posted, this one's tested :P sorry about that :)
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...
}
}
Okay, I think I get that. How would I go about calling the lines? would I go xmlDoc.Bob.1 for instance?
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
Follow this Question
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