1、安装builder这个gem
gem install builder
2、创建xml示例
require 'builder'
xml = Builder::XmlMarkup.new(:target =>$stdout,:indent => 1)
# :target =>$stdout 参数:指示输出内容将被写向标准输出控制台
# :indent =>1 参数:XML输出形式将被缩进一个空格字符
# x.comment! "书本信息" # <!-- 书本信息 -->
xml.instruct!# <?xml version="1.0" encoding="UTF-8"?>
xml.s(:Envelope,"xmlns:s"=>"http://schemas.xmlsoap.org/soap/envelope/"){
xml.s(:Body){
xml.Login(:xmlns=>'http://tempuri.org/'){
xml.user "LoginName"
xml.pw "LoginPw"
}
}
}
p xml #打印XML
3、输出结果
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Login xmlns="http://tempuri.org/">
<user>LoginName</user>
<pw>LoginPw</pw>
</Login>
</s:Body>
</s:Envelope>
<inspect/>