Ruby on rails, How to use Nokogiri to get a id in tags in XML?












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0


      1






      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 16:20









      YutingYuting

      52




      52
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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.






          share|improve this answer





















          • 1





            How is it HTML? What version of HTML contains tags channel, display-name and icon?

            – 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














          I think:



          doc = Nokogiri::HTML(info_html)
          channel = doc.css('channel')[0]['id']


          Checkout more about basic Nokogiri here in this link






          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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.






          share|improve this answer





















          • 1





            How is it HTML? What version of HTML contains tags channel, display-name and icon?

            – 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
















          0














          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.






          share|improve this answer





















          • 1





            How is it HTML? What version of HTML contains tags channel, display-name and icon?

            – 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














          0












          0








          0







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 28 '18 at 17:08

























          answered Dec 28 '18 at 16:39









          TiwTiw

          1,445618




          1,445618








          • 1





            How is it HTML? What version of HTML contains tags channel, display-name and icon?

            – 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





            How is it HTML? What version of HTML contains tags channel, display-name and icon?

            – 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













          1














          I think:



          doc = Nokogiri::HTML(info_html)
          channel = doc.css('channel')[0]['id']


          Checkout more about basic Nokogiri here in this link






          share|improve this answer
























          • 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
















          1














          I think:



          doc = Nokogiri::HTML(info_html)
          channel = doc.css('channel')[0]['id']


          Checkout more about basic Nokogiri here in this link






          share|improve this answer
























          • 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














          1












          1








          1







          I think:



          doc = Nokogiri::HTML(info_html)
          channel = doc.css('channel')[0]['id']


          Checkout more about basic Nokogiri here in this link






          share|improve this answer













          I think:



          doc = Nokogiri::HTML(info_html)
          channel = doc.css('channel')[0]['id']


          Checkout more about basic Nokogiri here in this link







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 16:28









          Lucas AndradeLucas Andrade

          355316




          355316













          • 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



















          • 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

















          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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas