회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


XML 파싱 (with Unity)

푸딩뱃살 | 2016.01.20 10:33 | 조회 7027
XML 파싱
:유니티에서 파싱하여 사용

-UTF-8로 꼭 저장
-요소들의 소대문자 구분
-Resources 폴더에서 관리하면 효율적

-xml 파싱 / 저장 / 로드
-Server에서 받기
-배경 배치된 것을 저장하여 재사용 가능 /  업데이트 유용
-xml 이외에 txt 파일도 파싱

아래에서 만든 맵 에디터의 정보를 xml 저장
XML에 저장해야 할 목록
1. width : 2, height : 2
2. style 정보(색상)
3. id 정보(오브젝트의 이름, x_y)

MapData01.xml
 

 
     
        0 
        0
        0
         
    
    
        1
        0
        0
        
    
    
        0
        1
        0
        
    
    
        1
        1
        0
        
    

파싱
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// xml, save, load 관련 클래스
using System.Xml;
using System.Xml.Serialization;
using System.Text; // save, load
using System.IO;

// cube 정보
public class Cube 
{
    [XmlElement("x")]
    public int x = 0;
    [XmlElement("y")]
    public int y = 0;
    [XmlElement("z")]
    public int z = 0;
    [XmlElement("style")]
    public int style = 0;
}

[XmlRoot("mapData")]
public class MapData {

    [XmlIgnore] // list 변수는 xml 형식에 사용을 하지 않는다.
    List<Cube> list = null;

    [XmlElement("cube")]
    public Cube[] cubeList
    {
        get
        {
            // list 담긴 값을 배열로 넘겨준다.
            return list.ToArray();
        }
        set // set 구현시 xml 파일을 로드 후 여기에 Cube 넣어준다.
        {
            // 배열로 넘겨준 데이터(Cube 배열)를 list로 담음
            list = new List<Cube>(value);
        }
    }
}


public class XMLManager : MonoBehaviour {

    void Start () {
        // xml 불러오기 용도
        // TextAsset => 텍스트 파일 관리하는 클래스
        TextAsset textAsset = Resources.Load("MapData/MapData01") as TextAsset;
        print(textAsset.text); // load된 텍스트 파일 출력

        // XmlSerializer() 형식을 관리
        XmlSerializer ser = new XmlSerializer(typeof(MapData));
        // Deserialize() xml -> class 형식으로 변환
        MapData mapData = ser.Deserialize(new StringReader(textAsset.text)) as MapData;

        foreach(Cube cube in mapData.cubeList)
        {
            print("x " + cube.x);
            print("y " + cube.y);
            print("z " + cube.z);
            print("style " + cube.style);
        }
    }
}
xml 저장
 // 중략

       List<Cube> list = new List<Cube>(); // 게임오브젝트 Cube 정보를 담을 List 공간

        // MapManager/child의 게임오브젝트(cube들)의 정보를 담음
        int childCount = mapManager.transform.childCount;
        for(int i = 0; i < childCount; i++)
        {
            Transform childTran = mapManager.transform.GetChild(i);
            int x = (int)childTran.position.x;
            int y = (int)childTran.position.y;
            int z = (int)childTran.position.z;
            int style = (int)childTran.gameObject.GetComponent<TileInfo>()._style; // style 얻어 오기

            Cube cube = new Cube();
            cube.x = x;
            cube.y = y;
            cube.z = z;
            cube.style = style;
            list.Add(cube); // list cube 데이터를 임시 보관 (xml에 옮기기 위해)
        }

        // xml저장하고자하는 데이터를 mapData에 담는다. (class 생성)
        MapData mapData = new MapData();
        mapData.cubeList = list.ToArray(); // 임시 보관한 list를 cubeList에 넣음

        // 저장 형식 지정
        XmlSerializer ser = new XmlSerializer(typeof(MapData));
        // StreamWriter(저장할 경로) : 저장 공간 확보(데이터를 차곡차곡 확보)
        TextWriter textWriter = new StreamWriter(path);
        // xml 파일 저장
        ser.Serialize(textWriter, mapData); // class -> xml 변환(저장)

// 중략

트러블슈팅

XmlException: Text node cannot appear in this state.


해결
  • 서버에 있는 xml파일이 정상적인지 확인

XmlException: Whitespace is required between PUBLIC id and SYSTEM id.


해결
  • 파일명이 맞는지 확인

285개(1/15페이지)
프로그래밍
번호 제목 글쓴이 조회 날짜
285 [Python] 동적 import - 모듈을 변수로 받아오기 푸딩뱃살 405 2022.10.27 10:45
284 [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 푸딩뱃살 440 2022.08.18 12:22
283 [Python] error: Microsoft Visual C++ 9.0 is required. 첨부파일 푸딩뱃살 659 2022.08.03 13:35
282 [Python] pyscript 첨부파일 푸딩뱃살 434 2022.06.09 11:21
281 [Python] float is / float not is 푸딩뱃살 581 2022.03.02 15:03
280 [Python] 이터널 문자열 f 푸딩뱃살 811 2022.01.27 16:35
279 [Python] is와 ==의 차이 푸딩뱃살 475 2021.11.25 15:54
278 [Python] Error: ImportError: file line 1: Ba 푸딩뱃살 891 2021.11.16 11:24
277 [Python] 파이썬 디컴파일 - uncompyle6 첨부파일 푸딩뱃살 750 2021.11.10 14:46
276 [Python] 파이썬 확장자 설명 푸딩뱃살 537 2021.11.03 14:38
275 [참고] 웹 fbx 뷰어 푸딩뱃살 456 2021.10.19 15:46
274 [Python] enumerate() 푸딩뱃살 487 2021.10.13 14:44
273 [Python] 아나콘다에서 가상 환경 첨부파일 푸딩뱃살 695 2020.11.21 00:26
272 [Python] pip로 설치 때 퍼미션 에러 사진 첨부파일 푸딩뱃살 1247 2020.06.06 17:13
271 [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio 사진 푸딩뱃살 657 2020.06.05 14:01
270 [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold 사진 푸딩뱃살 678 2020.06.05 13:58
269 [Python] OpenCV 10-1. 이미지 Thresholding 사진 푸딩뱃살 569 2020.06.05 13:56
268 [Python] OpenCV 9-2. 색 추적 푸딩뱃살 743 2020.06.02 23:29
267 [Python] OpenCV 9-1. 색공간 바꾸기 푸딩뱃살 630 2020.06.02 23:27
266 [Python] OpenCV 8-3. 이미지 비트 연산 사진 푸딩뱃살 512 2020.06.02 23:21