Ruby on rails, How to use Nokogiri to get a id in tags in XML?
recently I meet a issue with Nokogiri. how could I get the id in a tag?
for example, there is a xml file, and inside the code like this :
<channel id="firstchannel">
<display-name>channel name </display-name>
<icon src="pngpath"/>
</channel>
how could I get the id "firstchannel"?
Thank you in advance.
ruby-on-rails ruby xml nokogiri
add a comment |
recently I meet a issue with Nokogiri. how could I get the id in a tag?
for example, there is a xml file, and inside the code like this :
<channel id="firstchannel">
<display-name>channel name </display-name>
<icon src="pngpath"/>
</channel>
how could I get the id "firstchannel"?
Thank you in advance.
ruby-on-rails ruby xml nokogiri
add a comment |
recently I meet a issue with Nokogiri. how could I get the id in a tag?
for example, there is a xml file, and inside the code like this :
<channel id="firstchannel">
<display-name>channel name </display-name>
<icon src="pngpath"/>
</channel>
how could I get the id "firstchannel"?
Thank you in advance.
ruby-on-rails ruby xml nokogiri
recently I meet a issue with Nokogiri. how could I get the id in a tag?
for example, there is a xml file, and inside the code like this :
<channel id="firstchannel">
<display-name>channel name </display-name>
<icon src="pngpath"/>
</channel>
how could I get the id "firstchannel"?
Thank you in advance.
ruby-on-rails ruby xml nokogiri
ruby-on-rails ruby xml nokogiri
asked Dec 28 '18 at 16:20
YutingYuting
52
52
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are many different ways to locate the element you want.
For example, if
<icon src="pngpath"/>
is relatively unique and we use it as an anchor.
Then the code would be:
#require 'nokogiri'
doc = Nokogiri::XML File.read "file.xml" #Read xml file and parse into Nokogiri object
ic = doc.css('icon[src="pngpath"]') #locate icon element
theId = ic.first.parent.get_attribute :id #Find the id
p theId
#=> "firstchannel"
Depends on different situation, you might need different approach to find the
right thing you want.
1
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:05
1
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
|
show 5 more comments
I think:
doc = Nokogiri::HTML(info_html)
channel = doc.css('channel')[0]['id']
Checkout more about basic Nokogiri here in this link
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:06
Nokogiri doesn't check if theinfo_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.
– Lucas Andrade
Dec 28 '18 at 17:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961377%2fruby-on-rails-how-to-use-nokogiri-to-get-a-id-in-tags-in-xml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are many different ways to locate the element you want.
For example, if
<icon src="pngpath"/>
is relatively unique and we use it as an anchor.
Then the code would be:
#require 'nokogiri'
doc = Nokogiri::XML File.read "file.xml" #Read xml file and parse into Nokogiri object
ic = doc.css('icon[src="pngpath"]') #locate icon element
theId = ic.first.parent.get_attribute :id #Find the id
p theId
#=> "firstchannel"
Depends on different situation, you might need different approach to find the
right thing you want.
1
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:05
1
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
|
show 5 more comments
There are many different ways to locate the element you want.
For example, if
<icon src="pngpath"/>
is relatively unique and we use it as an anchor.
Then the code would be:
#require 'nokogiri'
doc = Nokogiri::XML File.read "file.xml" #Read xml file and parse into Nokogiri object
ic = doc.css('icon[src="pngpath"]') #locate icon element
theId = ic.first.parent.get_attribute :id #Find the id
p theId
#=> "firstchannel"
Depends on different situation, you might need different approach to find the
right thing you want.
1
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:05
1
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
|
show 5 more comments
There are many different ways to locate the element you want.
For example, if
<icon src="pngpath"/>
is relatively unique and we use it as an anchor.
Then the code would be:
#require 'nokogiri'
doc = Nokogiri::XML File.read "file.xml" #Read xml file and parse into Nokogiri object
ic = doc.css('icon[src="pngpath"]') #locate icon element
theId = ic.first.parent.get_attribute :id #Find the id
p theId
#=> "firstchannel"
Depends on different situation, you might need different approach to find the
right thing you want.
There are many different ways to locate the element you want.
For example, if
<icon src="pngpath"/>
is relatively unique and we use it as an anchor.
Then the code would be:
#require 'nokogiri'
doc = Nokogiri::XML File.read "file.xml" #Read xml file and parse into Nokogiri object
ic = doc.css('icon[src="pngpath"]') #locate icon element
theId = ic.first.parent.get_attribute :id #Find the id
p theId
#=> "firstchannel"
Depends on different situation, you might need different approach to find the
right thing you want.
edited Dec 28 '18 at 17:08
answered Dec 28 '18 at 16:39
TiwTiw
1,445618
1,445618
1
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:05
1
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
|
show 5 more comments
1
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:05
1
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
1
1
How is it
HTML
? What version of HTML contains tags channel
, display-name
and icon
?– Aleksei Matiushkin
Dec 28 '18 at 17:05
How is it
HTML
? What version of HTML contains tags channel
, display-name
and icon
?– Aleksei Matiushkin
Dec 28 '18 at 17:05
1
1
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
get_attribute :id is what I want , thank you .
– Yuting
Dec 28 '18 at 17:06
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
@AlekseiMatiushkin Thanks, updated. I think there're no difference in this case, am I wrong about this? Could you please tell me if there's any harm in what I did?
– Tiw
Dec 28 '18 at 17:11
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
No idea; it just looked semantically incorrect to me and I pointed that out.
– Aleksei Matiushkin
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
@AlekseiMatiushkin I see, thanks anyway :)
– Tiw
Dec 28 '18 at 17:13
|
show 5 more comments
I think:
doc = Nokogiri::HTML(info_html)
channel = doc.css('channel')[0]['id']
Checkout more about basic Nokogiri here in this link
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:06
Nokogiri doesn't check if theinfo_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.
– Lucas Andrade
Dec 28 '18 at 17:22
add a comment |
I think:
doc = Nokogiri::HTML(info_html)
channel = doc.css('channel')[0]['id']
Checkout more about basic Nokogiri here in this link
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:06
Nokogiri doesn't check if theinfo_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.
– Lucas Andrade
Dec 28 '18 at 17:22
add a comment |
I think:
doc = Nokogiri::HTML(info_html)
channel = doc.css('channel')[0]['id']
Checkout more about basic Nokogiri here in this link
I think:
doc = Nokogiri::HTML(info_html)
channel = doc.css('channel')[0]['id']
Checkout more about basic Nokogiri here in this link
answered Dec 28 '18 at 16:28
Lucas AndradeLucas Andrade
355316
355316
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:06
Nokogiri doesn't check if theinfo_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.
– Lucas Andrade
Dec 28 '18 at 17:22
add a comment |
How is itHTML
? What version of HTML contains tagschannel
,display-name
andicon
?
– Aleksei Matiushkin
Dec 28 '18 at 17:06
Nokogiri doesn't check if theinfo_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.
– Lucas Andrade
Dec 28 '18 at 17:22
How is it
HTML
? What version of HTML contains tags channel
, display-name
and icon
?– Aleksei Matiushkin
Dec 28 '18 at 17:06
How is it
HTML
? What version of HTML contains tags channel
, display-name
and icon
?– Aleksei Matiushkin
Dec 28 '18 at 17:06
Nokogiri doesn't check if the
info_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.– Lucas Andrade
Dec 28 '18 at 17:22
Nokogiri doesn't check if the
info_html
string is a valid HTML, he just expects HTML format, so with Yuting You XML example will work just fine. You also can try in your console.– Lucas Andrade
Dec 28 '18 at 17:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961377%2fruby-on-rails-how-to-use-nokogiri-to-get-a-id-in-tags-in-xml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown