Unity loaded the .XML file into the WWW class for XML parsing. Is there any function to de-serialize the information?(We are developing it for Android phones.)

Asked 1 years ago, Updated 1 years ago, 80 views

 IEnumerator read()

    {
        Text txt = GameObject.Find("ERRORMESSAGE2").GetComponent<Text>();

        string fileName = "Quests.xml";
        string filePath = "jar:file://" + Application.dataPath + "!/assets/" + fileName;

        var www = new WWW(filePath);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            txt.text = "Loading failed";
        }
        else if (string.IsNullOrEmpty(www.error))
        {
            txt.text = "Loading successful";
        }
    }

I called you in, but is there any function to serialize like iostream below?

 void serialize()

{

Type[ ] questType = { typeof(QUEST) };

        XmlSerializer serializer = new XmlSerializer(typeof(QuestContainer), questType);

        TextReader textReader = new StreamReader(filePath);
        QuestContainer = (QuestContainer)serializer.Deserialize(textReader);
        textReader.Close();
        txt.text = (textReader.ReadLine());
    }

We need the ability to de-serialize the information we have brought in to the form below.

The information is called up and then divided into each process into containers.

public class QuestContainer
{

    private List<QUEST> confirmed = new List<QUEST>();
    private List<QUEST> complete = new List<QUEST>();
    private List<QUEST> possible = new List<QUEST>();
    private List<QUEST> im_possible = new List<QUEST>();

    public List<QUEST> Confirmed
    {
        get { return confirmed; }
        set { confirmed = value; }
    }

    public List<QUEST> Complete
    {
        get { return complete; }
        set { complete = value; }
    }

    public List<QUEST> Possible
    {
        get { return possible; }
        set { possible = value; }
    }

    public List<QUEST> im_Possible
    {
        get { return im_possible; }
        set { im_possible = value; }
    }
}
public class QUEST
{
    public string Questname { get; set; }
    public string Description { get; set; }
    public string Class { get; set; }
    public int Level { get; set; }
    public int STR { get; set; }
    public int CON { get; set; }
    public int INT { get; set; }
    public int WIS { get; set; }
    public int DEX { get; set; }
    public string Title { get; set; }
    public string Item { get; set; }
    public QUEST_TYPE questType { get; set; }
    public DIFFICULTY difficulty { get; set; }
    public EXPEDIENT eXPEDIENT { get; set; }
    public QUEST_REWARD questReward { get; set; }
    public string target { get; set; }
    public int count { get; set; }
    public bool canClear { get; set; }

    public QUEST()
    {

    }

    public QUEST(string Questname,string Description,string Class,int level,int str,int con, int Int, int wis, int dex,string title,string item,  QUEST_TYPE QuestType, DIFFICULTY Difficulty, QUEST_REWARD quest_reward, EXPEDIENT EXPEDIENT, string Target, int Count, bool canclear)
    {
        this.Description = Description;
        this.Questname = Questname;
        this.questType = QuestType;
        this.difficulty = Difficulty;
        this.eXPEDIENT = EXPEDIENT;
        this.questReward = quest_reward;
        this.Class = Class;
        this.Level = level;
        this.STR = str;
        this.CON = con;
        this.INT = Int;
        this.WIS = wis;
        this.DEX = dex;
        this.Title = title;
        this.Item = item;
        this.target = Target;
        this.count = Count;
        this.canClear = canclear;
    }
<?xml version="1.0" encoding="utf-8"?>
<QuestContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Confirmed>
    <QUEST>
      <Questname>Start</Questname>
      <Description>It's just the beginning!</Description>
      <Class>ALL</Class>
      <Level>0</Level>
      <STR>0</STR>
      <CON>0</CON>
      <INT>0</INT>
      <WIS>0</WIS>
      <DEX>0</DEX>
      <Title />
      <Item />
      <questType>ROOP</questType>
      <difficulty>EASY</difficulty>
      <eXPEDIENT>MOVE</eXPEDIENT>
      <questReward>GOLD</questReward>
      <target>Bank</target>
      <count>10</count>
      <canClear>false</canClear>
    </QUEST>
  </Confirmed>
  <Complete />
  <Possible />
  <im_Possible />
</QuestContainer>

If you don't have such a function, I'd appreciate it if you could tell me how to inject it manually.

Additional Notes -

alt text

parsing xml unity

2022-09-22 22:13

4 Answers

We'd better use XmlSerialization. I checked from Android to loadXML(), and I checked the operation of write only in Unity Editor.

For your information, when you delete an item from the list, you should not delete it from the repeat statement. I made a tmp and erased it after moving it, so please check the code.

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;


public class NewBehaviourScript : MonoBehaviour {
    QuestContainer container;
    void Start () {
        loadXML ();

        List<QUEST> tmp = new List<QUEST> ();
        foreach (QUEST quest in container.Confirmed) {
            container.Complete.Add (quest);
            tmp.Add (quest);
        }

        foreach (QUEST quest in tmp) {
            container.Confirmed.Remove (quest);
        }

        writeXML ();


    }

    void loadXML(){
        TextAsset textAsset = (TextAsset) Resources.Load("Catalog");  
        var serializer = new XmlSerializer(typeof(QuestContainer));
        StringReader stream = new StringReader (textAsset.ToString ());
        container = serializer.Deserialize(stream) as QuestContainer;
        stream.Close();
        Debug.Log ("Confirmed");
        foreach (QUEST quest in container.Confirmed) {
            Debug.Log (quest.Questname);
        }

        Debug.Log ("Complete");
        foreach (QUEST quest in container.Complete) {
            Debug.Log (quest.Questname);
        }                                            
    }

    void writeXML(){
        var serializer = new XmlSerializer (typeof(QuestContainer));
        var stream = new FileStream(pathForDocumentsFile("tmp.xml"), FileMode.Create);
        serializer.Serialize(stream, container);
        stream.Close();
    }

    public string pathForDocumentsFile( string filename ) 
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
            path = path.Substring( 0, path.LastIndexOf( '/' ) );
            return Path.Combine( Path.Combine( path, "Documents" ), filename );
        }
        else if(Application.platform == RuntimePlatform.Android)
        {
            string path = Application.persistentDataPath; 
            path = path.Substring(0, path.LastIndexOf( '/' ) ); 
            return Path.Combine (path, filename);
        } 
        else 
        {
        string path = Application.dataPath; 
        path = path.Substring(0, path.LastIndexOf( '/' ) );
        return Path.Combine (path, filename);
        }
    }
}

Correction: I found a better way, so I don't think the method below is good.

Seeing that the code has an Inumerator...You were working at UNI.T. I think you can do it like this.

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
public class XMLParsing : MonoBehaviour{
  QuestContainer qc;
  void Awake(){
    qc = new QuestContainer();
  }

  void parseXML(Text txt){
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(txt);
        XmlNodeList xnList; 

    //Confirmed
    xnList= xml.SelectNodes("/QuestContainer/Confirmed/QUEST");    
    foreach(XmlNode node in xnList)
    {
      Quest quest = new Quest();
      quest.QuestName = xn["QuestName"].InnerText;
      quest.Level = int.Parse(xn["Level"].InnerText);
      qc.confirmed.Add(quest);
    }

    // Process Complete, Possible, and Impossible in the same way
  }
}


2022-09-22 22:13

To import files from Unity, you must place the files in the Resources folder. If you don't have a Resources folder under Assets, create it and put an XML file in it. Then, refer to the following code and make it read XML into QuestContainer. The xml file you can receive from < is in the Resources folder as Catalog.xml. For your information, you don't need to add an extension when you read the file from Resources.

It's a code that UNI.T played, so it'll work out well.

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public class NewBehaviourScript : MonoBehaviour {
    List<CD> confirmed= new List<CD>();

    // // Use this for initialization
    void Start () {
        parseXML ();


    }

    // // Update is called once per frame
    void Update () {

    }

    void parseXML(){

        TextAsset textAsset = (TextAsset) Resources.Load("Catalog");  
        XmlDocument xmlDoc = new XmlDocument ();
        xmlDoc.LoadXml ( textAsset.text );
        XmlNodeList xnList; 

        //Confirmed
        xnList= xmlDoc.SelectNodes("/CATALOG/CD");    
        foreach(XmlNode node in xnList)
        {

            CD cd = new CD();
            cd.title = node["TITLE"].InnerText;
            Debug.Log(cd.title);
            cd.artist = node["ARTIST"].InnerText;
            confirmed.Add(cd);
        }

        // Process Complete, Possible, and Impossible in the same way
    }
}

public class CD{
    public string title;
    public string artist;
}

When you post questions, I think you can explain it in more detail if you post them all in code, but since you upload the XML file as an image capture and you don't upload the definition of Quest class, I had no choice but to use another file.


2022-09-22 22:13

TextAsset textAsset = (TextAsset) Resources.Load("Catalog");  
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ( textAsset.text );

You said this route is not accessible. Is this how UNI.T created the folder structure? All files in the Resource are accessible even if you build them on Android.

alt text


2022-09-22 22:13

I built the code that I just uploaded and ran it, and it works well on Android. I don't know why it's not possible to read the contents in Resources in TextAsset. It's something that needs to be done.

I'm attaching the part that I recorded while turning it on my Android phone. In the Catalog.xml file, I was filming only Title with Debug.Log but I can see the name of the song clearly.

02-16 13:07:53.387    3637-3652/? D/Unity﹕ RGB GL_EXT_sRGB_write_control GL_EXT_texture_sRGB_decode GL_EXT_texture_filter_anisotropic GL_EXT_multisampled_render_to_texture GL_EXT_color_buffer_float GL_EXT_color_buffer_half_float GL_EXT_disjoint_timer_query
02-16 13:07:54.357    3637-3652/? I/Unity﹕ Empire Burlesque
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Hide your heart
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Greatest Hits
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Still got the blues
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Eros
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ One night only
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Sylvias Mother
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Maggie May
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Romanza
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ When a man loves a woman
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Black angel
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ 1999 Grammy Nominees
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ For the good times
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Big Willie style
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Tupelo Honey
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Soulsville
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ The very best of
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Stop
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Bridge of Spies
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Private Dancer
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Midt om natten
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Pavarotti Gala Concert
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ The dock of the bay
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Picture book
    (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
02-16 13:07:54.367    3637-3652/? I/Unity﹕ Red



2022-09-22 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.