Ruby XML到对象映射器

Ruby XML到对象映射器,ruby,xml,orm,dtd,Ruby,Xml,Orm,Dtd,我正在尝试制作一个应用程序文件格式解析器和生成器。应用程序使用带有自定义DTD的xml文件。目前,我正在考虑使用nokogiri编写对象映射器,将xml解析为对象,并使用这些对象生成xml。我尝试过HappyMapper和xml映射,但它们没有使用完整的xml格式。所以现在我已经做了这个,但我认为这是一个有点糟糕的设计 您可以将C#与XMLSerilaizer一起使用,它是最好的。以下是一个例子: ---------------just a lot of entity -------------

我正在尝试制作一个应用程序文件格式解析器和生成器。应用程序使用带有自定义DTD的xml文件。目前,我正在考虑使用
nokogiri
编写对象映射器,将xml解析为对象,并使用这些对象生成xml。我尝试过HappyMapper和xml映射,但它们没有使用完整的xml格式。所以现在我已经做了这个,但我认为这是一个有点糟糕的设计

您可以将C#与XMLSerilaizer一起使用,它是最好的。以下是一个例子:

---------------just a lot of entity ----------------------
using System;
namespace BuilderSerialization {
public class Address {
public Address() {}
public string Address1;
public string Address2;
public string City;
public string State;
public string Zip;
public string Country;
} }
using System;
namespace BuilderSerialization {
public class Author {
public Author() { }
public string FirstName;
public string MiddleName;
public string LastName;
public string Title;
public string Gender;
public Address AddressObject;
} }

namespace BuilderSerialization {
public class Book {
public Book() { }
public string Title;
public Author AuthorObject;
public string ISBN;
public double RetailPrice;
public string Publisher;
}}
-------------------------------------------------------
using System;
using System.Xml.Serialization;
using System.IO;
namespace BuilderSerialization {
class TestClass {
static void Main(string[] args) {
Book BookObject = new Book();
XmlSerializer ser = new XmlSerializer(typeof(Book));
TextWriter writer = new StreamWriter("booktest.xml");
BookObject.Title = "Practical LotusScript";
BookObject.ISBN = "1884777767 ";
BookObject.Publisher = "Manning Publications";
BookObject.RetailPrice = 43.95;
BookObject.AuthorObject = new Author();
BookObject.AuthorObject.FirstName = "Tony";
BookObject.AuthorObject.LastName = "Patton";
BookObject.AuthorObject.Gender = "Male";
BookObject.AuthorObject.AddressObject = new Address();
BookObject.AuthorObject.AddressObject.Address1 = "1 Main Street";
BookObject.AuthorObject.AddressObject.City = "Anywhere";
BookObject.AuthorObject.AddressObject.State = "KY";
BookObject.AuthorObject.AddressObject.Zip = "40000";
BookObject.AuthorObject.AddressObject.Country = "USA";
ser.Serialize(writer, BookObject);
writer.Close();
} } } 
之后,您将获得XML:

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Title>Practical LotusScript</Title>
<AuthorObject>
<FirstName>Tony</FirstName>
<LastName>Patton</LastName>
<Gender>Male</Gender>
<AddressObject>
<Address1>1 Main Street</Address1>
<City>Anywhere</City>
<State>KY</State>
<Zip>40000</Zip>
<Country>USA</Country>
</AddressObject>
</AuthorObject>
<ISBN>1884777767 </ISBN>
<RetailPrice>43.95</RetailPrice>
<Publisher>Manning Publications</Publisher>
</Book>

实用LotusScript
托尼
巴顿
男性
主街1号
在任何地方
基尼
40000
美国
1884777767
43.95
Manning出版的计算机书大全

我正在寻找最好的方法。现在我是这样做的:请在问题中包含您的源代码,而不是它的链接。如果链接中断,你的问题就不太有用了。OP正在寻找一个Ruby解决方案。如果你把IrRuby看作是一种Ruby解决方案,它仍然可以工作。