I know I can parse XML using Hpricot, but is it also possible to create files? All the tutorials I found only demonstrate parsing.
我知道我可以使用Hpricot解析XML,但是创建文件也可能吗?我找到的所有教程只演示解析。
2 个解决方案
#1
4
Nah. Hpricot is only for parsing XML. It does not allow to build documents.
不。Hpricot仅用于解析XML。它不允许构建文档。
But you might want to take a look at Nokogiri, which allows you to parse and build documents.
但是您可能想看看Nokogiri,它允许您解析和构建文档。
#2
5
Jim Weirich's Builder is very easy to use. Here's an example from Enterprise Integration with Ruby by Maik Schmidt:
Jim Weirich的建造者很容易使用。下面是Maik Schmidt关于企业与Ruby集成的一个例子:
xml = ''
doc = Builder::XmlMarkup.new(:target => xml, :indent => 2)
doc.instruct!
doc.person(:name => 'Max', :surname => 'Mustermann') { |person|
person.address { |address|
address.street('Hauptstr. 42')
address.tag!('postal-code', '12345')
address.city('Musterstadt')
}
person.position(:longitude => 12.345, :latitude => 56.789)
}
which produces:
生产:
<?xml version="1.0" encoding="UTF-8"?>
<person surname="Mustermann" name="Max">
<address>
<street>Hauptstr. 42</street>
<postal-code>12345</postal-code>
<city>Musterstadt</city>
</address>
<position longitude="12.345" latitude="56.789"/>
</person>
#1
4
Nah. Hpricot is only for parsing XML. It does not allow to build documents.
不。Hpricot仅用于解析XML。它不允许构建文档。
But you might want to take a look at Nokogiri, which allows you to parse and build documents.
但是您可能想看看Nokogiri,它允许您解析和构建文档。
#2
5
Jim Weirich's Builder is very easy to use. Here's an example from Enterprise Integration with Ruby by Maik Schmidt:
Jim Weirich的建造者很容易使用。下面是Maik Schmidt关于企业与Ruby集成的一个例子:
xml = ''
doc = Builder::XmlMarkup.new(:target => xml, :indent => 2)
doc.instruct!
doc.person(:name => 'Max', :surname => 'Mustermann') { |person|
person.address { |address|
address.street('Hauptstr. 42')
address.tag!('postal-code', '12345')
address.city('Musterstadt')
}
person.position(:longitude => 12.345, :latitude => 56.789)
}
which produces:
生产:
<?xml version="1.0" encoding="UTF-8"?>
<person surname="Mustermann" name="Max">
<address>
<street>Hauptstr. 42</street>
<postal-code>12345</postal-code>
<city>Musterstadt</city>
</address>
<position longitude="12.345" latitude="56.789"/>
</person>