2016-05-17 7 views
1

Ich schreibe ein Ruby-Skript, um einen Postman-SOAP-POST-Aufruf zu machen, dann Nokogiri zu verwenden, um die XML-Antwort zu analysieren. Als ich um die vollen SOAP-Aufruf-Antwort von Postman, es in mein Editor kopieren und manuell den XML Körper nehmen und dekodieren und zu formatieren es online das folgende Nokogiri Skript erfolgreich zu verwenden, ich bin in der Lage:Arbeiten mit XML-Körper von Soap-Aufruf mit Nokogiri in Ruby

doc = Nokogiri::XML(File.open("response.xml")) 

property_ids = [] 

doc.css('Property').each do |property| 
    puts "Property ID: #{property['PropertyId']}" 
    property_ids << property['PropertyId'] 
end 


property_ids.each_with_index do |property_id, index| 
    puts "index: #{index}" 
    puts "property id: #{property_id}" 
end 

Wo ich laufen in das Problem ist, wenn ich im Script die Ruby-Schnipsel des Postman Anruf aufnehmen möchten:

require 'nokogiri' 
require 'uri' 
require 'net/http' 
require 'openssl' 


url = URI("https://esite.thelyndco.com/AmsiWeb/eDexWeb/esite/leasing.asmx") 

http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

request = Net::HTTP::Post.new(url) 
request["content-type"] = 'application/soap+xml' 
request["cache-control"] = 'no-cache' 
request["postman-token"] = '916e3f3d-11ca-e8cf-2066-542b009a281d' 
request.body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n <soap12:Body>\r\n <GetPropertyList xmlns=\"http://tempuri.org/\">\r\n  <UserID>updater</UserID>\r\n  <Password>[password]</Password>\r\n  <PortfolioName>[portfolio name]</PortfolioName>\r\n  <XMLData> \r\n</XMLData>\r\n </GetPropertyList>\r\n </soap12:Body>\r\n</soap12:Envelope>" 

response = http.request(request) 

doc = Nokogiri::XML(response.body) 
# doc = Nokogiri::XML(File.open("full-response.xml")) 
# doc.at('GetPropertyListResponse').text 

Was will ich mit dem SOAP-Umschlag die vollständige SOAP-Antwort ist, um zu tun und in der Lage, sie zu verarbeiten in mein Skript, ohne ausschneiden und einfügen zu müssen; manuell dekodieren und formatieren mit Online-XML-Formatierern.

Kommentiert sind ein paar Zeilen, die ich von Stack Overflow probiert habe. Ist es möglich, den XML-Körper mit Nokogiri zu entschlüsseln und zu formatieren oder den SOAP-Umschlag auszuwerten?

edit:

Durch die XML-Decodierung ich meine Einnahme:

<GetPropertyListResult>&lt;Properties&gt;&lt;Property PropertyId="11A" PropertyName1="1111 Austin Hwy" PropertyName2="" PropertyAddrLine1="The 1111" PropertyAddrLine2="1111 Austin Highway" PropertyAddrLine3="" PropertyAddrLine4="" PropertyAddrCity="San Antonio" PropertyAddrState="TX" PropertyAddrZipCode="78209" PropertyAddrCountry="" PropertyAddrEmail="" RemitToAddrLine1="The 1111" RemitToAddrLine2="1111 Austin Highway" RemitToAddrLine3="" RemitToAddrLine4="" RemitToAddrCity="San Antonio" RemitToAddrState="TX" RemitToAddrZipCode="78209" RemitToAddrCountry="" LiveDate="2013-12-04T00:00:00" MgrOffPhoneNo="210-804-1100" MgrFaxNo="" MgrSalutation="" MgrFirstName="" MgrMiName="" MgrLastName="" MonthEndInProcess="N"&gt;&lt;Amenity PropertyId="11A" 

und Dekodieren es in die Nutzung dieser online XML decoder:

<GetPropertyListResult><Properties><Property PropertyId="11A" PropertyName1="1111 Austin Hwy" PropertyName2="" PropertyAddrLine1="The 1111" PropertyAddrLine2="1111 Austin Highway" PropertyAddrLine3="" PropertyAddrLine4="" PropertyAddrCity="San Antonio" PropertyAddrState="TX" PropertyAddrZipCode="78209" PropertyAddrCountry="" PropertyAddrEmail="" RemitToAddrLine1="The 1111" RemitToAddrLine2="1111 Austin Highway" RemitToAddrLine3="" RemitToAddrLine4="" RemitToAddrCity="San Antonio" RemitToAddrState="TX" RemitToAddrZipCode="78209" RemitToAddrCountry="" LiveDate="2013-12-04T00:00:00" MgrOffPhoneNo="210-804-1100" MgrFaxNo="" MgrSalutation="" MgrFirstName="" MgrMiName="" MgrLastName="" MonthEndInProcess="N"><Amenity PropertyId="11A" 

dann durch eine XML formatter ausgeführt, so dass verschachtelte Elemente sind für die Lesbarkeit eingerückt.

+0

Was die ‚decode‘ Verben tun und ‚format‘ bedeutet in diesem Fall? Was machst du eigentlich mit der Hand, die du mit dem Ruby-Code machen musst? –

+0

@MichaelGaskill, bearbeitete Frage zur Klärung der XML-Decodierung und Formatierung. – phizzy

Antwort

1

können Sie diesen Code verwenden, um die XML-zu dekodieren und Format:

require "nokogiri" 

XML_CHAR_ENTITIES = { 
    "lt" => "<", 
    "gt" => ">", 
    "amp" => "&", 
    "num" => "#", 
    "comma" => "," 
} 

xsl =<<XSL 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/"> 
    <xsl:copy-of select="."/> 
    </xsl:template> 
</xsl:stylesheet> 
XSL 


xml = '<GetPropertyListResult>&lt;Properties&gt;&lt;Property PropertyId="11A" PropertyName1="1111 Austin Hwy" PropertyName2="" PropertyAddrLine1="The 1111" PropertyAddrLine2="1111 Austin Highway" PropertyAddrLine3="" PropertyAddrLine4="" PropertyAddrCity="San Antonio" PropertyAddrState="TX" PropertyAddrZipCode="78209" PropertyAddrCountry="" PropertyAddrEmail="" RemitToAddrLine1="The 1111" RemitToAddrLine2="1111 Austin Highway" RemitToAddrLine3="" RemitToAddrLine4="" RemitToAddrCity="San Antonio" RemitToAddrState="TX" RemitToAddrZipCode="78209" RemitToAddrCountry="" LiveDate="2013-12-04T00:00:00" MgrOffPhoneNo="210-804-1100" MgrFaxNo="" MgrSalutation="" MgrFirstName="" MgrMiName="" MgrLastName="" MonthEndInProcess="N"&gt;&lt;Amenity PropertyId="11A"></GetPropertyListResult>' 

xml = xml.gsub(/&(\w+);/) do |match| 
    char_entity = XML_CHAR_ENTITIES[$1] 
    char_entity ? char_entity : match 
end 

doc = Nokogiri::XML(xml) 
xslt = Nokogiri::XSLT(xsl) 
xml = xslt.transform(doc) 

puts "#{xml}" 

Die XML wurde unvollständig zur Verfügung gestellt, so dass dieser Abschluss String angehängt wurde, damit es analysiert werden: ></GetPropertyListResult>

Die XML_CHAR_ENTITIES bieten ein Hash von codierten Zeichenfolgen zu decodierten Zeichenfolgen und kann leicht erweitert werden, um andere XML-Zeichenentitäten einzubeziehen, wie beispielsweise die, die unter der Nummer W3 Character Entity Reference Chart dokumentiert sind.

XSL ist ein eingebettetes Stylesheet, das zum Formatieren des XML für die Ausgabe mit Nokogiri verwendet wird.

Die Decodierung der XML-Zeichenentitäten erfolgt mit dem Aufruf String#gsub mit der Blockoption. Das XML wird dann erfolgreich von Nokogiri analysiert. Sobald der XML-Code analysiert wurde, wird er mithilfe der Nokogiri-XSLT-Transformation formatiert.

Der Ausgang dieses Codes ist:

<?xml version="1.0" encoding="UTF-8"?> 
<GetPropertyListResult> 
    <Properties> 
    <Property PropertyId="11A" PropertyName1="1111 Austin Hwy" PropertyName2="" PropertyAddrLine1="The 1111" PropertyAddrLine2="1111 Austin Highway" PropertyAddrLine3="" PropertyAddrLine4="" PropertyAddrCity="San Antonio" PropertyAddrState="TX" PropertyAddrZipCode="78209" PropertyAddrCountry="" PropertyAddrEmail="" RemitToAddrLine1="The 1111" RemitToAddrLine2="1111 Austin Highway" RemitToAddrLine3="" RemitToAddrLine4="" RemitToAddrCity="San Antonio" RemitToAddrState="TX" RemitToAddrZipCode="78209" RemitToAddrCountry="" LiveDate="2013-12-04T00:00:00" MgrOffPhoneNo="210-804-1100" MgrFaxNo="" MgrSalutation="" MgrFirstName="" MgrMiName="" MgrLastName="" MonthEndInProcess="N"> 
     <Amenity PropertyId="11A"/> 
    </Property> 
    </Properties> 
</GetPropertyListResult>