2012-11-17 247 views
0

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

http://pastie.org/5393012

+0

所以你有什么问题吗? –

+0

我正在寻找最好的方法来做到这一点。现在我正在这样做:https://github.com/gcds/libeagle –

+0

请在问题中包含您的源代码,而不是指向它的链接。如果/当链接打破你的问题将不会很有用。 –

回答

-1

您可以使用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> 
+0

OP正在寻找一个Ruby解决方案。 –

+0

如果你认为ironruby是一种红宝石解决方案,它仍然可以工作。 –