Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

/xml.diff Secret

Created August 12, 2015 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2f7eb3e1c01fa6f4df2a to your computer and use it in GitHub Desktop.
Save anonymous/2f7eb3e1c01fa6f4df2a to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Message.pm b/lib/Mojo/Message.pm
index 56d5cd9..23b9fbe 100644
--- a/lib/Mojo/Message.pm
+++ b/lib/Mojo/Message.pm
@@ -66,7 +66,8 @@ sub cookies { croak 'Method "cookies" not implemented by subclass' }
sub dom {
my $self = shift;
return undef if $self->content->is_multipart;
- my $dom = $self->{dom} ||= Mojo::DOM->new($self->text);
+ my $dom = $self->{dom}
+ ||= Mojo::DOM->new->xml($self->{xml})->parse($self->text);
return @_ ? $dom->find(@_) : $dom;
}
@@ -216,6 +217,8 @@ sub uploads {
return \@uploads;
}
+sub xml { ++$_[0]->{xml} and return $_[0] }
+
sub _build {
my ($self, $method) = @_;
@@ -491,6 +494,9 @@ to parse it, so you have to make sure it is not excessively large, there's a
say $msg->dom->at('title')->text;
say $msg->dom->at('body')->children->map('tag')->uniq->join("\n");
+ # Work with XML
+ say $msg->xml->dom->at('svg circle')->{href};
+
=head2 error
my $err = $msg->error;
@@ -659,6 +665,12 @@ All C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects.
# Names of all uploads
say $_->name for @{$msg->uploads};
+=head2 x-www-form-urlencoded
+
+ $msg = $msg->xml;
+
+Activate XML mode for L</"dom">.
+
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
diff --git a/t/mojo/response.t b/t/mojo/response.t
index f563531..ceed325 100644
--- a/t/mojo/response.t
+++ b/t/mojo/response.t
@@ -1058,6 +1058,7 @@ ok $res->is_finished, 'response is finished';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
+ok !$res->dom->xml, 'XML mode not active';
is $res->dom->at('p')->text, 'foo', 'right value';
is $res->dom->at('p > a')->text, 'bar', 'right value';
is $res->dom('p')->first->text, 'foo', 'right value';
@@ -1075,6 +1076,20 @@ is_deeply \@text, [qw(test test)], 'right values';
is_deeply $res->dom->find('p > a')->map('text')->to_array, [qw(test test)],
'right values';
+# Parse response and extract XML
+$res = Mojo::Message::Response->new;
+$res->parse("HTTP/1.1 200 OK\x0a");
+$res->parse("Content-Type: text/xml\x0a");
+$res->parse("Content-Length: 45\x0a\x0a");
+$res->parse('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
+ok $res->is_finished, 'response is finished';
+is $res->code, 200, 'right status';
+is $res->message, 'OK', 'right message';
+is $res->version, '1.1', 'right version';
+ok $res->xml->dom->xml, 'XML mode not active';
+is $res->dom('P')->first->text, 'Hi!', 'right value';
+is $res->dom->at('P[ID=greeting]')->text, 'Hi!', 'right value';
+
# Build DOM from response with charset
$res = Mojo::Message::Response->new;
$res->parse("HTTP/1.0 200 OK\x0a");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment