posted by 써니루루 2007. 3. 26. 10:44

읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE booklist SYSTEM "booklist.dtd">

<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
 
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
 
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
 
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>


Javascript에서 DOM을 이용해본 사람이라면 익숙할 getElementById() 메소드를 이용하는 동일한 사용법으로 이용할 수 있다.

ID를 이용해서 테그를 선택해야하는데. 단, HTML DOM은 이미 ID라는 attribute가 정의 되어 있지만 xml에서는 id attribute가 정해져 있는 것이 아니다.

따라서 위 xml파일과 같이 dtd 파일에서 attribute를 ID특성으로 정의 해줘야 한다.

참조하는 DTD 파일은 다음과 같다. 'booklist.dtd'

<?xml version="1.0" encoding="utf-8" ?>
<!ELEMENT booklist (book*)>
<!ELEMENT book (title,author,publisher,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT price (#PCDATA)>

<!ATTLIST book
  id  ID  #REQUIRED
  kind  CDATA #IMPLIED>


이를 이용한 C# 코드는 다음과 같다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p562_FindElementById
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement element = xDoc.GetElementById("b2");

            string title = element.FirstChild.FirstChild.Value;
            Console.WriteLine(title);
        }
    }
}


ref. .NET 닷넷 개발자를 위한 XML p.562
 

posted by 써니루루 2007. 3. 26. 10:21
읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>


이번 예제는 XmlAttributeCollection을 이용해 element에 속한 attribute를 collection으로 가져와서 loop를 이용해 출력하는 예제를보여준다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p559_FindAttribute
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBook = xDoc.DocumentElement;
            XmlElement eFirstBook = (XmlElement)eBook.FirstChild;

            XmlAttributeCollection attributes = eFirstBook.Attributes;

            // loop를 이용한 attribute 탐색
            for (int i = 0; i < attributes.Count; i++)
            {
                XmlAttribute attribute = (XmlAttribute)attributes[i];
                Console.WriteLine(attribute.Name + "\t" + attribute.Value);
            }

            // 직접 attribute 출력
            string id = eFirstBook.GetAttribute("id");
            Console.WriteLine("id:\t" + id);

            string kind = eFirstBook.GetAttribute("kind");
            Console.WriteLine("kind:\t" + kind);
        }
    }
}

attribute를 탐색할때 위 예제와 같이 활용하면 되겠다.

ref. .NET 닷넷 개발자를 위한 XML p.559

posted by 써니루루 2007. 3. 26. 10:01
읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>


이번 예제는 NextSibling, PreviousSibling을 이용해서 Element의 앞, 뒤 노드를 탐색하는 예제입니다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p557
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBook = xDoc.DocumentElement;
            XmlElement eFirstBook = (XmlElement)eBook.FirstChild;
            XmlElement eNextBook = (XmlElement)eFirstBook.NextSibling;

            XmlElement eLastBook = (XmlElement)eBook.LastChild;
            XmlElement ePreviousBook = (XmlElement)eLastBook.PreviousSibling;

            if (eNextBook == ePreviousBook)
            {
                Console.WriteLine("동일한 Book 엘리먼트 입니다.");
            }
            else
            {
                Console.WriteLine("다른 Book 엘리먼트 입니다.");
            }
        }
    }
}



위 원본 XML 파일에 book 노드가 4개이고,
첫노드의 다음 노드는 b2 book이고, 마지막노드의 이전 노드는 b3 book이 된다.
따라서 두개의 book 엘리먼트는 다른 엘리먼트가 된다.

ref. .NET 닷넷 개발자를 위한 XML p.557