Scripting: what is the easiest to extract a value in a tag of a XML file?












12















I want to read a pom.xml ('Project Object Model' of Maven) and extract the version information. Here is an example:



<?xml version="1.0" encoding="UTF-8"?><project 
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<name>project-parent</name>
<version>1.0.74-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconnect</artifactId>
<version>6.05-26023</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>


How can I extract the version '1.0.74-SNAPSHOT' from above?



Would love to be able to do so using simple bash scripting sed or awk. Otherwise a simple python is preferred.



EDIT





  1. Constraint



    The linux box is in a corporate environment so I can only use tools that are already installed (not that I cannot request utility such as xml2, but I have to go through a lot of red-tape). Some of the solutions are very good (learn a few new tricks already), but they may not be applicable due to the restricted environment




  2. updated xml listing



    I added the dependencies tag to the original listing. This will show some hacky solution may not work in this case




  3. Distro



    The distro I am using is RHEL4












share|improve this question

























  • Is this stackoverflow.com/questions/29004/… sufficient?

    – bbaja42
    Dec 20 '11 at 22:08











  • Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version'

    – Anthony Kong
    Dec 20 '11 at 22:20











  • Which xml-related tools and libraries are available? Are jvm-based soltuions OK?

    – Vi.
    Dec 20 '11 at 23:22











  • So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4.

    – Anthony Kong
    Dec 20 '11 at 23:38













  • (I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... stackoverflow.com/questions/2735548/…

    – JStrahl
    Jan 18 '13 at 10:12
















12















I want to read a pom.xml ('Project Object Model' of Maven) and extract the version information. Here is an example:



<?xml version="1.0" encoding="UTF-8"?><project 
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<name>project-parent</name>
<version>1.0.74-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconnect</artifactId>
<version>6.05-26023</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>


How can I extract the version '1.0.74-SNAPSHOT' from above?



Would love to be able to do so using simple bash scripting sed or awk. Otherwise a simple python is preferred.



EDIT





  1. Constraint



    The linux box is in a corporate environment so I can only use tools that are already installed (not that I cannot request utility such as xml2, but I have to go through a lot of red-tape). Some of the solutions are very good (learn a few new tricks already), but they may not be applicable due to the restricted environment




  2. updated xml listing



    I added the dependencies tag to the original listing. This will show some hacky solution may not work in this case




  3. Distro



    The distro I am using is RHEL4












share|improve this question

























  • Is this stackoverflow.com/questions/29004/… sufficient?

    – bbaja42
    Dec 20 '11 at 22:08











  • Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version'

    – Anthony Kong
    Dec 20 '11 at 22:20











  • Which xml-related tools and libraries are available? Are jvm-based soltuions OK?

    – Vi.
    Dec 20 '11 at 23:22











  • So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4.

    – Anthony Kong
    Dec 20 '11 at 23:38













  • (I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... stackoverflow.com/questions/2735548/…

    – JStrahl
    Jan 18 '13 at 10:12














12












12








12


7






I want to read a pom.xml ('Project Object Model' of Maven) and extract the version information. Here is an example:



<?xml version="1.0" encoding="UTF-8"?><project 
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<name>project-parent</name>
<version>1.0.74-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconnect</artifactId>
<version>6.05-26023</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>


How can I extract the version '1.0.74-SNAPSHOT' from above?



Would love to be able to do so using simple bash scripting sed or awk. Otherwise a simple python is preferred.



EDIT





  1. Constraint



    The linux box is in a corporate environment so I can only use tools that are already installed (not that I cannot request utility such as xml2, but I have to go through a lot of red-tape). Some of the solutions are very good (learn a few new tricks already), but they may not be applicable due to the restricted environment




  2. updated xml listing



    I added the dependencies tag to the original listing. This will show some hacky solution may not work in this case




  3. Distro



    The distro I am using is RHEL4












share|improve this question
















I want to read a pom.xml ('Project Object Model' of Maven) and extract the version information. Here is an example:



<?xml version="1.0" encoding="UTF-8"?><project 
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>project-parent</artifactId>
<name>project-parent</name>
<version>1.0.74-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconnect</artifactId>
<version>6.05-26023</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>


How can I extract the version '1.0.74-SNAPSHOT' from above?



Would love to be able to do so using simple bash scripting sed or awk. Otherwise a simple python is preferred.



EDIT





  1. Constraint



    The linux box is in a corporate environment so I can only use tools that are already installed (not that I cannot request utility such as xml2, but I have to go through a lot of red-tape). Some of the solutions are very good (learn a few new tricks already), but they may not be applicable due to the restricted environment




  2. updated xml listing



    I added the dependencies tag to the original listing. This will show some hacky solution may not work in this case




  3. Distro



    The distro I am using is RHEL4









linux bash unix python xml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '16 at 8:44









Cyrus

3,83611024




3,83611024










asked Dec 20 '11 at 22:01









Anthony KongAnthony Kong

1,53363458




1,53363458













  • Is this stackoverflow.com/questions/29004/… sufficient?

    – bbaja42
    Dec 20 '11 at 22:08











  • Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version'

    – Anthony Kong
    Dec 20 '11 at 22:20











  • Which xml-related tools and libraries are available? Are jvm-based soltuions OK?

    – Vi.
    Dec 20 '11 at 23:22











  • So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4.

    – Anthony Kong
    Dec 20 '11 at 23:38













  • (I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... stackoverflow.com/questions/2735548/…

    – JStrahl
    Jan 18 '13 at 10:12



















  • Is this stackoverflow.com/questions/29004/… sufficient?

    – bbaja42
    Dec 20 '11 at 22:08











  • Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version'

    – Anthony Kong
    Dec 20 '11 at 22:20











  • Which xml-related tools and libraries are available? Are jvm-based soltuions OK?

    – Vi.
    Dec 20 '11 at 23:22











  • So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4.

    – Anthony Kong
    Dec 20 '11 at 23:38













  • (I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... stackoverflow.com/questions/2735548/…

    – JStrahl
    Jan 18 '13 at 10:12

















Is this stackoverflow.com/questions/29004/… sufficient?

– bbaja42
Dec 20 '11 at 22:08





Is this stackoverflow.com/questions/29004/… sufficient?

– bbaja42
Dec 20 '11 at 22:08













Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version'

– Anthony Kong
Dec 20 '11 at 22:20





Not really. There are a lot of version tag in the xml (e.g. under dependencies tag). I only want '/project/version'

– Anthony Kong
Dec 20 '11 at 22:20













Which xml-related tools and libraries are available? Are jvm-based soltuions OK?

– Vi.
Dec 20 '11 at 23:22





Which xml-related tools and libraries are available? Are jvm-based soltuions OK?

– Vi.
Dec 20 '11 at 23:22













So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4.

– Anthony Kong
Dec 20 '11 at 23:38







So far I can tell xml2, xmlgrep and perl XML module are not present. Most unix command-line utilities are present. The distro is Redhat EL 4.

– Anthony Kong
Dec 20 '11 at 23:38















(I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... stackoverflow.com/questions/2735548/…

– JStrahl
Jan 18 '13 at 10:12





(I couldn't add a comment so I have to reply as an answer, overkill somewhat) Some great answers can be found here..... stackoverflow.com/questions/2735548/…

– JStrahl
Jan 18 '13 at 10:12










14 Answers
14






active

oldest

votes


















16














xml2 can convert xml to/from line-oriented format:



xml2 < pom.xml  | grep /project/version= | sed 's/.*=//'





share|improve this answer































    6














    Using python



    $ python -c 'from xml.etree.ElementTree import ElementTree; print ElementTree(file="pom.xml").findtext("{http://maven.apache.org/POM/4.0.0}version")'
    1.0.74-SNAPSHOT


    Using xmlstarlet



    $ xml sel -N x="http://maven.apache.org/POM/4.0.0" -t -m 'x:project/x:version' -v . pom.xml
    1.0.74-SNAPSHOT


    Using xmllint



    $ echo -e 'setns x=http://maven.apache.org/POM/4.0.0ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /
    1.0.74-SNAPSHOT





    share|improve this answer


























    • cat (//x:version)[1]/text() when using xmllint also works!

      – kev
      Dec 21 '11 at 5:50



















    5














    Other way: xmlgrep and XPath:



    xmlgrep --text_only '/project/version' pom.xml


    Disadvantage: slow






    share|improve this answer

































      5














      Clojure way. Requires only jvm with special jar file:



      java -cp clojure.jar clojure.main -e "(use 'clojure.xml) (->> (java.io.File. "pom.xml") (clojure.xml/parse) (:content) (filter #(= (:tag %) :version)) (first) (:content) (first) (println))"


      Scala way:



      java -Xbootclasspath/a:scala-library.jar -cp scala-compiler.jar scala.tools.nsc.MainGenericRunner -e 'import scala.xml._; println((XML.load(new java.io.FileInputStream("pom.xml")) match { case <project>{children @ _*}</project> => for (i <- children if (i  match { case <version>{children @ _*}</version> => true; case _ => false;  }))  yield i })(0) match { case <version>{Text(x)}</version> => x })'


      Groovy way:



      java -classpath groovy-all.jar groovy.ui.GroovyMain -e 'println (new XmlParser().parse(new File("pom.xml")).value().findAll({ it.name().getLocalPart()=="version" }).first().value().first())'





      share|improve this answer


























      • This is awesome! Great idea!

        – Anthony Kong
        Dec 21 '11 at 0:06



















      4














      Here's an alternative in Perl



      $ perl -MXML::Simple -e'print XMLin("pom.xml")->{version}."n"'
      1.0.74-SNAPSHOT


      It works with the revised/extended example in the questions which has multiple "version" elements at different depths.






      share|improve this answer


























      • Slow, (although faster than xmlgrep)

        – Vi.
        Dec 20 '11 at 22:58





















      3














      Hacky way:



      perl -e '$_ = join "", <>; m!<project[^>]*>.*n(?:    |t)<version[^>]*>s*([^<]+?)s*</version>.*</project>!s and print "$1n"' pom.xml


      Relies on correct indentation of the required <version>






      share|improve this answer


























      • Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

        – Anthony Kong
        Dec 20 '11 at 23:14











      • Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

        – Vi.
        Dec 20 '11 at 23:17













      • Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

        – Vi.
        Dec 20 '11 at 23:18











      • Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

        – Anthony Kong
        Dec 20 '11 at 23:22











      • What about doing it from Java? Using pom files implies having JVM installed.

        – Vi.
        Dec 20 '11 at 23:25



















      3














      Work out a very clumsy, one-liner solution



      python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [n for n in dom.getElementsByTagName('version') if n.parentNode == dom.childNodes[0]][0].toxml()" | sed -e "s/.*>(.*)<.*/1/g"


      The sed at the end is very ugly but i was not able to print out the text of the node with mindom alone.



      Update from _Vi:



      Less hacky Python version:



      python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [i.childNodes.item(0).nodeValue for i in dom.firstChild.childNodes if i.nodeName == 'version'].pop()"


      Update from me



      Another version:



          python -c "from  xml.dom.minidom import parse;dom = parse('pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"





      share|improve this answer

































        2














        XSLT way:



        <?xml version="1.0" encoding="ISO-8859-1"?>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>

        <xsl:template match="/">
        <xsl:for-each select="*[local-name()='project']">
        <xsl:for-each select="*[local-name()='version']">
        <xsl:value-of select="text()"/>
        </xsl:for-each>
        </xsl:for-each>
        </xsl:template>
        </xsl:stylesheet>




        xalan -xsl x.xsl -in pom.xml





        share|improve this answer
























        • If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

          – fpmurphy
          Dec 21 '11 at 5:12





















        2














        if "There are a lot of version tag in the xml" then you better forget about doing it with "simple tools" and regexps, that won't do.



        try this python (no dependencies):



        from xml.dom.minidom import parse

        dom = parse('pom.xml')
        project = dom.getElementsByTagName('project')[0]
        for node in project.childNodes:
        if node.nodeType == node.ELEMENT_NODE and node.tagName == 'version':
        print node.firstChild.nodeValue





        share|improve this answer
























        • What exactly does this script do?

          – Simon Sheehan
          Dec 22 '11 at 1:41











        • it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

          – Samus_
          Dec 22 '11 at 15:17





















        1














        Here is a one-liner using sed:



        sed '/<dependencies>/,/</dependencies>/d;/<version>/!d;s/ *</?version> *//g' pom.xml





        share|improve this answer



















        • 1





          Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

          – Vi.
          Dec 21 '11 at 16:33



















        0














        Return_text_val=$(xmllint --xpath "//*[local-name()='$TagElmnt']" $FILE )


        Here, try this:



        $TagElmnt - TagName
        $FILE - xml file to parse





        share|improve this answer

































          0














          sed -n "/<name>project-parent/{n;s/.*>(.*)<.*/1/p;q}" pom.xml


          The -n option avoids printing non-matching lines; first match (/.../) is on the line before the one with wanted text; the n command skips to next line, where s extracts relevant info thru a capturing group ((...)), and a backreference (1). p prints out, q quits.






          share|improve this answer





















          • 2





            Can you expand your answer to explain this? Thanks.

            – fixer1234
            Oct 27 '15 at 1:42



















          0














          awk works fine without using any extra tools.
          cat pod.xml



          <project>
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.networks.app</groupId>
          <artifactId>operation-platform</artifactId>
          <version>1.0.0</version>
          <packaging>tar.xz</packaging>
          <description>POM was created by Sonatype Nexus</description>
          </project>


          simple and legible way to get the value of <packaging> tag:



          cat pod.xml | awk -F'[<>]' '/packaging/{print $3}'





          share|improve this answer































            -1














            I know your question says Linux but if you have the need to do this on Windows without the need of any 3rd party tools such that you can put it in a batch file, Powershell can extract any node from the your pom.xml file like so:



            powershell -Command "& {select-xml //pom:project/pom:properties/pom:mypluginversion -path pom.xml -Namespace  @{pom='http://maven.apache.org/POM/4.0.0'} | foreach {$_.Node.Innerxml}}" > myPluginVersion.txt





            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              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%2fsuperuser.com%2fquestions%2f369996%2fscripting-what-is-the-easiest-to-extract-a-value-in-a-tag-of-a-xml-file%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              14 Answers
              14






              active

              oldest

              votes








              14 Answers
              14






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              16














              xml2 can convert xml to/from line-oriented format:



              xml2 < pom.xml  | grep /project/version= | sed 's/.*=//'





              share|improve this answer




























                16














                xml2 can convert xml to/from line-oriented format:



                xml2 < pom.xml  | grep /project/version= | sed 's/.*=//'





                share|improve this answer


























                  16












                  16








                  16







                  xml2 can convert xml to/from line-oriented format:



                  xml2 < pom.xml  | grep /project/version= | sed 's/.*=//'





                  share|improve this answer













                  xml2 can convert xml to/from line-oriented format:



                  xml2 < pom.xml  | grep /project/version= | sed 's/.*=//'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 20 '11 at 22:21









                  Vi.Vi.

                  7,7892083163




                  7,7892083163

























                      6














                      Using python



                      $ python -c 'from xml.etree.ElementTree import ElementTree; print ElementTree(file="pom.xml").findtext("{http://maven.apache.org/POM/4.0.0}version")'
                      1.0.74-SNAPSHOT


                      Using xmlstarlet



                      $ xml sel -N x="http://maven.apache.org/POM/4.0.0" -t -m 'x:project/x:version' -v . pom.xml
                      1.0.74-SNAPSHOT


                      Using xmllint



                      $ echo -e 'setns x=http://maven.apache.org/POM/4.0.0ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /
                      1.0.74-SNAPSHOT





                      share|improve this answer


























                      • cat (//x:version)[1]/text() when using xmllint also works!

                        – kev
                        Dec 21 '11 at 5:50
















                      6














                      Using python



                      $ python -c 'from xml.etree.ElementTree import ElementTree; print ElementTree(file="pom.xml").findtext("{http://maven.apache.org/POM/4.0.0}version")'
                      1.0.74-SNAPSHOT


                      Using xmlstarlet



                      $ xml sel -N x="http://maven.apache.org/POM/4.0.0" -t -m 'x:project/x:version' -v . pom.xml
                      1.0.74-SNAPSHOT


                      Using xmllint



                      $ echo -e 'setns x=http://maven.apache.org/POM/4.0.0ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /
                      1.0.74-SNAPSHOT





                      share|improve this answer


























                      • cat (//x:version)[1]/text() when using xmllint also works!

                        – kev
                        Dec 21 '11 at 5:50














                      6












                      6








                      6







                      Using python



                      $ python -c 'from xml.etree.ElementTree import ElementTree; print ElementTree(file="pom.xml").findtext("{http://maven.apache.org/POM/4.0.0}version")'
                      1.0.74-SNAPSHOT


                      Using xmlstarlet



                      $ xml sel -N x="http://maven.apache.org/POM/4.0.0" -t -m 'x:project/x:version' -v . pom.xml
                      1.0.74-SNAPSHOT


                      Using xmllint



                      $ echo -e 'setns x=http://maven.apache.org/POM/4.0.0ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /
                      1.0.74-SNAPSHOT





                      share|improve this answer















                      Using python



                      $ python -c 'from xml.etree.ElementTree import ElementTree; print ElementTree(file="pom.xml").findtext("{http://maven.apache.org/POM/4.0.0}version")'
                      1.0.74-SNAPSHOT


                      Using xmlstarlet



                      $ xml sel -N x="http://maven.apache.org/POM/4.0.0" -t -m 'x:project/x:version' -v . pom.xml
                      1.0.74-SNAPSHOT


                      Using xmllint



                      $ echo -e 'setns x=http://maven.apache.org/POM/4.0.0ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /
                      1.0.74-SNAPSHOT






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 21 '11 at 5:39

























                      answered Dec 21 '11 at 4:54









                      kevkev

                      7,39964260




                      7,39964260













                      • cat (//x:version)[1]/text() when using xmllint also works!

                        – kev
                        Dec 21 '11 at 5:50



















                      • cat (//x:version)[1]/text() when using xmllint also works!

                        – kev
                        Dec 21 '11 at 5:50

















                      cat (//x:version)[1]/text() when using xmllint also works!

                      – kev
                      Dec 21 '11 at 5:50





                      cat (//x:version)[1]/text() when using xmllint also works!

                      – kev
                      Dec 21 '11 at 5:50











                      5














                      Other way: xmlgrep and XPath:



                      xmlgrep --text_only '/project/version' pom.xml


                      Disadvantage: slow






                      share|improve this answer






























                        5














                        Other way: xmlgrep and XPath:



                        xmlgrep --text_only '/project/version' pom.xml


                        Disadvantage: slow






                        share|improve this answer




























                          5












                          5








                          5







                          Other way: xmlgrep and XPath:



                          xmlgrep --text_only '/project/version' pom.xml


                          Disadvantage: slow






                          share|improve this answer















                          Other way: xmlgrep and XPath:



                          xmlgrep --text_only '/project/version' pom.xml


                          Disadvantage: slow







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 20 '11 at 23:10

























                          answered Dec 20 '11 at 22:43









                          Vi.Vi.

                          7,7892083163




                          7,7892083163























                              5














                              Clojure way. Requires only jvm with special jar file:



                              java -cp clojure.jar clojure.main -e "(use 'clojure.xml) (->> (java.io.File. "pom.xml") (clojure.xml/parse) (:content) (filter #(= (:tag %) :version)) (first) (:content) (first) (println))"


                              Scala way:



                              java -Xbootclasspath/a:scala-library.jar -cp scala-compiler.jar scala.tools.nsc.MainGenericRunner -e 'import scala.xml._; println((XML.load(new java.io.FileInputStream("pom.xml")) match { case <project>{children @ _*}</project> => for (i <- children if (i  match { case <version>{children @ _*}</version> => true; case _ => false;  }))  yield i })(0) match { case <version>{Text(x)}</version> => x })'


                              Groovy way:



                              java -classpath groovy-all.jar groovy.ui.GroovyMain -e 'println (new XmlParser().parse(new File("pom.xml")).value().findAll({ it.name().getLocalPart()=="version" }).first().value().first())'





                              share|improve this answer


























                              • This is awesome! Great idea!

                                – Anthony Kong
                                Dec 21 '11 at 0:06
















                              5














                              Clojure way. Requires only jvm with special jar file:



                              java -cp clojure.jar clojure.main -e "(use 'clojure.xml) (->> (java.io.File. "pom.xml") (clojure.xml/parse) (:content) (filter #(= (:tag %) :version)) (first) (:content) (first) (println))"


                              Scala way:



                              java -Xbootclasspath/a:scala-library.jar -cp scala-compiler.jar scala.tools.nsc.MainGenericRunner -e 'import scala.xml._; println((XML.load(new java.io.FileInputStream("pom.xml")) match { case <project>{children @ _*}</project> => for (i <- children if (i  match { case <version>{children @ _*}</version> => true; case _ => false;  }))  yield i })(0) match { case <version>{Text(x)}</version> => x })'


                              Groovy way:



                              java -classpath groovy-all.jar groovy.ui.GroovyMain -e 'println (new XmlParser().parse(new File("pom.xml")).value().findAll({ it.name().getLocalPart()=="version" }).first().value().first())'





                              share|improve this answer


























                              • This is awesome! Great idea!

                                – Anthony Kong
                                Dec 21 '11 at 0:06














                              5












                              5








                              5







                              Clojure way. Requires only jvm with special jar file:



                              java -cp clojure.jar clojure.main -e "(use 'clojure.xml) (->> (java.io.File. "pom.xml") (clojure.xml/parse) (:content) (filter #(= (:tag %) :version)) (first) (:content) (first) (println))"


                              Scala way:



                              java -Xbootclasspath/a:scala-library.jar -cp scala-compiler.jar scala.tools.nsc.MainGenericRunner -e 'import scala.xml._; println((XML.load(new java.io.FileInputStream("pom.xml")) match { case <project>{children @ _*}</project> => for (i <- children if (i  match { case <version>{children @ _*}</version> => true; case _ => false;  }))  yield i })(0) match { case <version>{Text(x)}</version> => x })'


                              Groovy way:



                              java -classpath groovy-all.jar groovy.ui.GroovyMain -e 'println (new XmlParser().parse(new File("pom.xml")).value().findAll({ it.name().getLocalPart()=="version" }).first().value().first())'





                              share|improve this answer















                              Clojure way. Requires only jvm with special jar file:



                              java -cp clojure.jar clojure.main -e "(use 'clojure.xml) (->> (java.io.File. "pom.xml") (clojure.xml/parse) (:content) (filter #(= (:tag %) :version)) (first) (:content) (first) (println))"


                              Scala way:



                              java -Xbootclasspath/a:scala-library.jar -cp scala-compiler.jar scala.tools.nsc.MainGenericRunner -e 'import scala.xml._; println((XML.load(new java.io.FileInputStream("pom.xml")) match { case <project>{children @ _*}</project> => for (i <- children if (i  match { case <version>{children @ _*}</version> => true; case _ => false;  }))  yield i })(0) match { case <version>{Text(x)}</version> => x })'


                              Groovy way:



                              java -classpath groovy-all.jar groovy.ui.GroovyMain -e 'println (new XmlParser().parse(new File("pom.xml")).value().findAll({ it.name().getLocalPart()=="version" }).first().value().first())'






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 21 '11 at 9:25

























                              answered Dec 21 '11 at 0:00









                              Vi.Vi.

                              7,7892083163




                              7,7892083163













                              • This is awesome! Great idea!

                                – Anthony Kong
                                Dec 21 '11 at 0:06



















                              • This is awesome! Great idea!

                                – Anthony Kong
                                Dec 21 '11 at 0:06

















                              This is awesome! Great idea!

                              – Anthony Kong
                              Dec 21 '11 at 0:06





                              This is awesome! Great idea!

                              – Anthony Kong
                              Dec 21 '11 at 0:06











                              4














                              Here's an alternative in Perl



                              $ perl -MXML::Simple -e'print XMLin("pom.xml")->{version}."n"'
                              1.0.74-SNAPSHOT


                              It works with the revised/extended example in the questions which has multiple "version" elements at different depths.






                              share|improve this answer


























                              • Slow, (although faster than xmlgrep)

                                – Vi.
                                Dec 20 '11 at 22:58


















                              4














                              Here's an alternative in Perl



                              $ perl -MXML::Simple -e'print XMLin("pom.xml")->{version}."n"'
                              1.0.74-SNAPSHOT


                              It works with the revised/extended example in the questions which has multiple "version" elements at different depths.






                              share|improve this answer


























                              • Slow, (although faster than xmlgrep)

                                – Vi.
                                Dec 20 '11 at 22:58
















                              4












                              4








                              4







                              Here's an alternative in Perl



                              $ perl -MXML::Simple -e'print XMLin("pom.xml")->{version}."n"'
                              1.0.74-SNAPSHOT


                              It works with the revised/extended example in the questions which has multiple "version" elements at different depths.






                              share|improve this answer















                              Here's an alternative in Perl



                              $ perl -MXML::Simple -e'print XMLin("pom.xml")->{version}."n"'
                              1.0.74-SNAPSHOT


                              It works with the revised/extended example in the questions which has multiple "version" elements at different depths.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 21 '11 at 16:50

























                              answered Dec 20 '11 at 22:45









                              RedGrittyBrickRedGrittyBrick

                              67k13106161




                              67k13106161













                              • Slow, (although faster than xmlgrep)

                                – Vi.
                                Dec 20 '11 at 22:58





















                              • Slow, (although faster than xmlgrep)

                                – Vi.
                                Dec 20 '11 at 22:58



















                              Slow, (although faster than xmlgrep)

                              – Vi.
                              Dec 20 '11 at 22:58







                              Slow, (although faster than xmlgrep)

                              – Vi.
                              Dec 20 '11 at 22:58













                              3














                              Hacky way:



                              perl -e '$_ = join "", <>; m!<project[^>]*>.*n(?:    |t)<version[^>]*>s*([^<]+?)s*</version>.*</project>!s and print "$1n"' pom.xml


                              Relies on correct indentation of the required <version>






                              share|improve this answer


























                              • Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

                                – Anthony Kong
                                Dec 20 '11 at 23:14











                              • Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

                                – Vi.
                                Dec 20 '11 at 23:17













                              • Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

                                – Vi.
                                Dec 20 '11 at 23:18











                              • Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

                                – Anthony Kong
                                Dec 20 '11 at 23:22











                              • What about doing it from Java? Using pom files implies having JVM installed.

                                – Vi.
                                Dec 20 '11 at 23:25
















                              3














                              Hacky way:



                              perl -e '$_ = join "", <>; m!<project[^>]*>.*n(?:    |t)<version[^>]*>s*([^<]+?)s*</version>.*</project>!s and print "$1n"' pom.xml


                              Relies on correct indentation of the required <version>






                              share|improve this answer


























                              • Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

                                – Anthony Kong
                                Dec 20 '11 at 23:14











                              • Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

                                – Vi.
                                Dec 20 '11 at 23:17













                              • Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

                                – Vi.
                                Dec 20 '11 at 23:18











                              • Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

                                – Anthony Kong
                                Dec 20 '11 at 23:22











                              • What about doing it from Java? Using pom files implies having JVM installed.

                                – Vi.
                                Dec 20 '11 at 23:25














                              3












                              3








                              3







                              Hacky way:



                              perl -e '$_ = join "", <>; m!<project[^>]*>.*n(?:    |t)<version[^>]*>s*([^<]+?)s*</version>.*</project>!s and print "$1n"' pom.xml


                              Relies on correct indentation of the required <version>






                              share|improve this answer















                              Hacky way:



                              perl -e '$_ = join "", <>; m!<project[^>]*>.*n(?:    |t)<version[^>]*>s*([^<]+?)s*</version>.*</project>!s and print "$1n"' pom.xml


                              Relies on correct indentation of the required <version>







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 23 '17 at 12:41









                              Community

                              1




                              1










                              answered Dec 20 '11 at 22:55









                              Vi.Vi.

                              7,7892083163




                              7,7892083163













                              • Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

                                – Anthony Kong
                                Dec 20 '11 at 23:14











                              • Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

                                – Vi.
                                Dec 20 '11 at 23:17













                              • Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

                                – Vi.
                                Dec 20 '11 at 23:18











                              • Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

                                – Anthony Kong
                                Dec 20 '11 at 23:22











                              • What about doing it from Java? Using pom files implies having JVM installed.

                                – Vi.
                                Dec 20 '11 at 23:25



















                              • Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

                                – Anthony Kong
                                Dec 20 '11 at 23:14











                              • Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

                                – Vi.
                                Dec 20 '11 at 23:17













                              • Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

                                – Vi.
                                Dec 20 '11 at 23:18











                              • Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

                                – Anthony Kong
                                Dec 20 '11 at 23:22











                              • What about doing it from Java? Using pom files implies having JVM installed.

                                – Vi.
                                Dec 20 '11 at 23:25

















                              Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

                              – Anthony Kong
                              Dec 20 '11 at 23:14





                              Thanks for the suggestion, but unfortunately it will not return what I want. Please see the updated pom model.

                              – Anthony Kong
                              Dec 20 '11 at 23:14













                              Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

                              – Vi.
                              Dec 20 '11 at 23:17







                              Returns "1.0.74-SNAPSHOT". Note that I changed the script after reading about multiple <version> things.

                              – Vi.
                              Dec 20 '11 at 23:17















                              Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

                              – Vi.
                              Dec 20 '11 at 23:18





                              Note: this solution is provided "just for fun" and is not intended to be used in actual product. Better use xml2/xmlgrep/XML::Simple solution.

                              – Vi.
                              Dec 20 '11 at 23:18













                              Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

                              – Anthony Kong
                              Dec 20 '11 at 23:22





                              Thanks! even though it is 'just for fun' but it is probably the 'most suitable' solution by far because it has minimum number of dependencies: It only requires perl ;-)

                              – Anthony Kong
                              Dec 20 '11 at 23:22













                              What about doing it from Java? Using pom files implies having JVM installed.

                              – Vi.
                              Dec 20 '11 at 23:25





                              What about doing it from Java? Using pom files implies having JVM installed.

                              – Vi.
                              Dec 20 '11 at 23:25











                              3














                              Work out a very clumsy, one-liner solution



                              python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [n for n in dom.getElementsByTagName('version') if n.parentNode == dom.childNodes[0]][0].toxml()" | sed -e "s/.*>(.*)<.*/1/g"


                              The sed at the end is very ugly but i was not able to print out the text of the node with mindom alone.



                              Update from _Vi:



                              Less hacky Python version:



                              python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [i.childNodes.item(0).nodeValue for i in dom.firstChild.childNodes if i.nodeName == 'version'].pop()"


                              Update from me



                              Another version:



                                  python -c "from  xml.dom.minidom import parse;dom = parse('pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"





                              share|improve this answer






























                                3














                                Work out a very clumsy, one-liner solution



                                python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [n for n in dom.getElementsByTagName('version') if n.parentNode == dom.childNodes[0]][0].toxml()" | sed -e "s/.*>(.*)<.*/1/g"


                                The sed at the end is very ugly but i was not able to print out the text of the node with mindom alone.



                                Update from _Vi:



                                Less hacky Python version:



                                python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [i.childNodes.item(0).nodeValue for i in dom.firstChild.childNodes if i.nodeName == 'version'].pop()"


                                Update from me



                                Another version:



                                    python -c "from  xml.dom.minidom import parse;dom = parse('pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"





                                share|improve this answer




























                                  3












                                  3








                                  3







                                  Work out a very clumsy, one-liner solution



                                  python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [n for n in dom.getElementsByTagName('version') if n.parentNode == dom.childNodes[0]][0].toxml()" | sed -e "s/.*>(.*)<.*/1/g"


                                  The sed at the end is very ugly but i was not able to print out the text of the node with mindom alone.



                                  Update from _Vi:



                                  Less hacky Python version:



                                  python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [i.childNodes.item(0).nodeValue for i in dom.firstChild.childNodes if i.nodeName == 'version'].pop()"


                                  Update from me



                                  Another version:



                                      python -c "from  xml.dom.minidom import parse;dom = parse('pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"





                                  share|improve this answer















                                  Work out a very clumsy, one-liner solution



                                  python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [n for n in dom.getElementsByTagName('version') if n.parentNode == dom.childNodes[0]][0].toxml()" | sed -e "s/.*>(.*)<.*/1/g"


                                  The sed at the end is very ugly but i was not able to print out the text of the node with mindom alone.



                                  Update from _Vi:



                                  Less hacky Python version:



                                  python -c "from xml.dom.minidom import parse;dom = parse('pom.xml');print [i.childNodes.item(0).nodeValue for i in dom.firstChild.childNodes if i.nodeName == 'version'].pop()"


                                  Update from me



                                  Another version:



                                      python -c "from  xml.dom.minidom import parse;dom = parse('pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Dec 29 '11 at 6:09

























                                  answered Dec 20 '11 at 23:24









                                  Anthony KongAnthony Kong

                                  1,53363458




                                  1,53363458























                                      2














                                      XSLT way:



                                      <?xml version="1.0" encoding="ISO-8859-1"?>
                                      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                                      <xsl:output method="text"/>

                                      <xsl:template match="/">
                                      <xsl:for-each select="*[local-name()='project']">
                                      <xsl:for-each select="*[local-name()='version']">
                                      <xsl:value-of select="text()"/>
                                      </xsl:for-each>
                                      </xsl:for-each>
                                      </xsl:template>
                                      </xsl:stylesheet>




                                      xalan -xsl x.xsl -in pom.xml





                                      share|improve this answer
























                                      • If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

                                        – fpmurphy
                                        Dec 21 '11 at 5:12


















                                      2














                                      XSLT way:



                                      <?xml version="1.0" encoding="ISO-8859-1"?>
                                      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                                      <xsl:output method="text"/>

                                      <xsl:template match="/">
                                      <xsl:for-each select="*[local-name()='project']">
                                      <xsl:for-each select="*[local-name()='version']">
                                      <xsl:value-of select="text()"/>
                                      </xsl:for-each>
                                      </xsl:for-each>
                                      </xsl:template>
                                      </xsl:stylesheet>




                                      xalan -xsl x.xsl -in pom.xml





                                      share|improve this answer
























                                      • If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

                                        – fpmurphy
                                        Dec 21 '11 at 5:12
















                                      2












                                      2








                                      2







                                      XSLT way:



                                      <?xml version="1.0" encoding="ISO-8859-1"?>
                                      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                                      <xsl:output method="text"/>

                                      <xsl:template match="/">
                                      <xsl:for-each select="*[local-name()='project']">
                                      <xsl:for-each select="*[local-name()='version']">
                                      <xsl:value-of select="text()"/>
                                      </xsl:for-each>
                                      </xsl:for-each>
                                      </xsl:template>
                                      </xsl:stylesheet>




                                      xalan -xsl x.xsl -in pom.xml





                                      share|improve this answer













                                      XSLT way:



                                      <?xml version="1.0" encoding="ISO-8859-1"?>
                                      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                                      <xsl:output method="text"/>

                                      <xsl:template match="/">
                                      <xsl:for-each select="*[local-name()='project']">
                                      <xsl:for-each select="*[local-name()='version']">
                                      <xsl:value-of select="text()"/>
                                      </xsl:for-each>
                                      </xsl:for-each>
                                      </xsl:template>
                                      </xsl:stylesheet>




                                      xalan -xsl x.xsl -in pom.xml






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 21 '11 at 1:16









                                      Vi.Vi.

                                      7,7892083163




                                      7,7892083163













                                      • If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

                                        – fpmurphy
                                        Dec 21 '11 at 5:12





















                                      • If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

                                        – fpmurphy
                                        Dec 21 '11 at 5:12



















                                      If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

                                      – fpmurphy
                                      Dec 21 '11 at 5:12







                                      If xsltproc is on your system, and it probably is as libxslt is on RHEL4, then you can use it and the above stylesheet to output the tag, i.e. xsltproc x.xsl prom.xsl.

                                      – fpmurphy
                                      Dec 21 '11 at 5:12













                                      2














                                      if "There are a lot of version tag in the xml" then you better forget about doing it with "simple tools" and regexps, that won't do.



                                      try this python (no dependencies):



                                      from xml.dom.minidom import parse

                                      dom = parse('pom.xml')
                                      project = dom.getElementsByTagName('project')[0]
                                      for node in project.childNodes:
                                      if node.nodeType == node.ELEMENT_NODE and node.tagName == 'version':
                                      print node.firstChild.nodeValue





                                      share|improve this answer
























                                      • What exactly does this script do?

                                        – Simon Sheehan
                                        Dec 22 '11 at 1:41











                                      • it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

                                        – Samus_
                                        Dec 22 '11 at 15:17


















                                      2














                                      if "There are a lot of version tag in the xml" then you better forget about doing it with "simple tools" and regexps, that won't do.



                                      try this python (no dependencies):



                                      from xml.dom.minidom import parse

                                      dom = parse('pom.xml')
                                      project = dom.getElementsByTagName('project')[0]
                                      for node in project.childNodes:
                                      if node.nodeType == node.ELEMENT_NODE and node.tagName == 'version':
                                      print node.firstChild.nodeValue





                                      share|improve this answer
























                                      • What exactly does this script do?

                                        – Simon Sheehan
                                        Dec 22 '11 at 1:41











                                      • it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

                                        – Samus_
                                        Dec 22 '11 at 15:17
















                                      2












                                      2








                                      2







                                      if "There are a lot of version tag in the xml" then you better forget about doing it with "simple tools" and regexps, that won't do.



                                      try this python (no dependencies):



                                      from xml.dom.minidom import parse

                                      dom = parse('pom.xml')
                                      project = dom.getElementsByTagName('project')[0]
                                      for node in project.childNodes:
                                      if node.nodeType == node.ELEMENT_NODE and node.tagName == 'version':
                                      print node.firstChild.nodeValue





                                      share|improve this answer













                                      if "There are a lot of version tag in the xml" then you better forget about doing it with "simple tools" and regexps, that won't do.



                                      try this python (no dependencies):



                                      from xml.dom.minidom import parse

                                      dom = parse('pom.xml')
                                      project = dom.getElementsByTagName('project')[0]
                                      for node in project.childNodes:
                                      if node.nodeType == node.ELEMENT_NODE and node.tagName == 'version':
                                      print node.firstChild.nodeValue






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 22 '11 at 1:38









                                      Samus_Samus_

                                      1662




                                      1662













                                      • What exactly does this script do?

                                        – Simon Sheehan
                                        Dec 22 '11 at 1:41











                                      • it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

                                        – Samus_
                                        Dec 22 '11 at 15:17





















                                      • What exactly does this script do?

                                        – Simon Sheehan
                                        Dec 22 '11 at 1:41











                                      • it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

                                        – Samus_
                                        Dec 22 '11 at 15:17



















                                      What exactly does this script do?

                                      – Simon Sheehan
                                      Dec 22 '11 at 1:41





                                      What exactly does this script do?

                                      – Simon Sheehan
                                      Dec 22 '11 at 1:41













                                      it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

                                      – Samus_
                                      Dec 22 '11 at 15:17







                                      it loads the XML as a DOM structure using Python's minidom implementation: docs.python.org/library/xml.dom.minidom.html the idea is to grab the <project> tag that is unique and then iterate over its child nodes (direct childs only) to find the tag <version> that we're looking for and not other tags with the same name in other places.

                                      – Samus_
                                      Dec 22 '11 at 15:17













                                      1














                                      Here is a one-liner using sed:



                                      sed '/<dependencies>/,/</dependencies>/d;/<version>/!d;s/ *</?version> *//g' pom.xml





                                      share|improve this answer



















                                      • 1





                                        Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

                                        – Vi.
                                        Dec 21 '11 at 16:33
















                                      1














                                      Here is a one-liner using sed:



                                      sed '/<dependencies>/,/</dependencies>/d;/<version>/!d;s/ *</?version> *//g' pom.xml





                                      share|improve this answer



















                                      • 1





                                        Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

                                        – Vi.
                                        Dec 21 '11 at 16:33














                                      1












                                      1








                                      1







                                      Here is a one-liner using sed:



                                      sed '/<dependencies>/,/</dependencies>/d;/<version>/!d;s/ *</?version> *//g' pom.xml





                                      share|improve this answer













                                      Here is a one-liner using sed:



                                      sed '/<dependencies>/,/</dependencies>/d;/<version>/!d;s/ *</?version> *//g' pom.xml






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 21 '11 at 15:53









                                      chickenkillerchickenkiller

                                      26113




                                      26113








                                      • 1





                                        Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

                                        – Vi.
                                        Dec 21 '11 at 16:33














                                      • 1





                                        Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

                                        – Vi.
                                        Dec 21 '11 at 16:33








                                      1




                                      1





                                      Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

                                      – Vi.
                                      Dec 21 '11 at 16:33





                                      Relies on absence of parameters in elements and that extra <version>s can be only inside dependencies.

                                      – Vi.
                                      Dec 21 '11 at 16:33











                                      0














                                      Return_text_val=$(xmllint --xpath "//*[local-name()='$TagElmnt']" $FILE )


                                      Here, try this:



                                      $TagElmnt - TagName
                                      $FILE - xml file to parse





                                      share|improve this answer






























                                        0














                                        Return_text_val=$(xmllint --xpath "//*[local-name()='$TagElmnt']" $FILE )


                                        Here, try this:



                                        $TagElmnt - TagName
                                        $FILE - xml file to parse





                                        share|improve this answer




























                                          0












                                          0








                                          0







                                          Return_text_val=$(xmllint --xpath "//*[local-name()='$TagElmnt']" $FILE )


                                          Here, try this:



                                          $TagElmnt - TagName
                                          $FILE - xml file to parse





                                          share|improve this answer















                                          Return_text_val=$(xmllint --xpath "//*[local-name()='$TagElmnt']" $FILE )


                                          Here, try this:



                                          $TagElmnt - TagName
                                          $FILE - xml file to parse






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited May 13 '15 at 13:08









                                          Kunal

                                          1,59051726




                                          1,59051726










                                          answered May 13 '15 at 11:41









                                          VijayababuVijayababu

                                          1




                                          1























                                              0














                                              sed -n "/<name>project-parent/{n;s/.*>(.*)<.*/1/p;q}" pom.xml


                                              The -n option avoids printing non-matching lines; first match (/.../) is on the line before the one with wanted text; the n command skips to next line, where s extracts relevant info thru a capturing group ((...)), and a backreference (1). p prints out, q quits.






                                              share|improve this answer





















                                              • 2





                                                Can you expand your answer to explain this? Thanks.

                                                – fixer1234
                                                Oct 27 '15 at 1:42
















                                              0














                                              sed -n "/<name>project-parent/{n;s/.*>(.*)<.*/1/p;q}" pom.xml


                                              The -n option avoids printing non-matching lines; first match (/.../) is on the line before the one with wanted text; the n command skips to next line, where s extracts relevant info thru a capturing group ((...)), and a backreference (1). p prints out, q quits.






                                              share|improve this answer





















                                              • 2





                                                Can you expand your answer to explain this? Thanks.

                                                – fixer1234
                                                Oct 27 '15 at 1:42














                                              0












                                              0








                                              0







                                              sed -n "/<name>project-parent/{n;s/.*>(.*)<.*/1/p;q}" pom.xml


                                              The -n option avoids printing non-matching lines; first match (/.../) is on the line before the one with wanted text; the n command skips to next line, where s extracts relevant info thru a capturing group ((...)), and a backreference (1). p prints out, q quits.






                                              share|improve this answer















                                              sed -n "/<name>project-parent/{n;s/.*>(.*)<.*/1/p;q}" pom.xml


                                              The -n option avoids printing non-matching lines; first match (/.../) is on the line before the one with wanted text; the n command skips to next line, where s extracts relevant info thru a capturing group ((...)), and a backreference (1). p prints out, q quits.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Oct 29 '15 at 1:27

























                                              answered Oct 26 '15 at 23:04









                                              SΛLVΘSΛLVΘ

                                              1,0471610




                                              1,0471610








                                              • 2





                                                Can you expand your answer to explain this? Thanks.

                                                – fixer1234
                                                Oct 27 '15 at 1:42














                                              • 2





                                                Can you expand your answer to explain this? Thanks.

                                                – fixer1234
                                                Oct 27 '15 at 1:42








                                              2




                                              2





                                              Can you expand your answer to explain this? Thanks.

                                              – fixer1234
                                              Oct 27 '15 at 1:42





                                              Can you expand your answer to explain this? Thanks.

                                              – fixer1234
                                              Oct 27 '15 at 1:42











                                              0














                                              awk works fine without using any extra tools.
                                              cat pod.xml



                                              <project>
                                              <modelVersion>4.0.0</modelVersion>
                                              <groupId>com.networks.app</groupId>
                                              <artifactId>operation-platform</artifactId>
                                              <version>1.0.0</version>
                                              <packaging>tar.xz</packaging>
                                              <description>POM was created by Sonatype Nexus</description>
                                              </project>


                                              simple and legible way to get the value of <packaging> tag:



                                              cat pod.xml | awk -F'[<>]' '/packaging/{print $3}'





                                              share|improve this answer




























                                                0














                                                awk works fine without using any extra tools.
                                                cat pod.xml



                                                <project>
                                                <modelVersion>4.0.0</modelVersion>
                                                <groupId>com.networks.app</groupId>
                                                <artifactId>operation-platform</artifactId>
                                                <version>1.0.0</version>
                                                <packaging>tar.xz</packaging>
                                                <description>POM was created by Sonatype Nexus</description>
                                                </project>


                                                simple and legible way to get the value of <packaging> tag:



                                                cat pod.xml | awk -F'[<>]' '/packaging/{print $3}'





                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  awk works fine without using any extra tools.
                                                  cat pod.xml



                                                  <project>
                                                  <modelVersion>4.0.0</modelVersion>
                                                  <groupId>com.networks.app</groupId>
                                                  <artifactId>operation-platform</artifactId>
                                                  <version>1.0.0</version>
                                                  <packaging>tar.xz</packaging>
                                                  <description>POM was created by Sonatype Nexus</description>
                                                  </project>


                                                  simple and legible way to get the value of <packaging> tag:



                                                  cat pod.xml | awk -F'[<>]' '/packaging/{print $3}'





                                                  share|improve this answer













                                                  awk works fine without using any extra tools.
                                                  cat pod.xml



                                                  <project>
                                                  <modelVersion>4.0.0</modelVersion>
                                                  <groupId>com.networks.app</groupId>
                                                  <artifactId>operation-platform</artifactId>
                                                  <version>1.0.0</version>
                                                  <packaging>tar.xz</packaging>
                                                  <description>POM was created by Sonatype Nexus</description>
                                                  </project>


                                                  simple and legible way to get the value of <packaging> tag:



                                                  cat pod.xml | awk -F'[<>]' '/packaging/{print $3}'






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jan 17 at 4:45









                                                  user5723841user5723841

                                                  1011




                                                  1011























                                                      -1














                                                      I know your question says Linux but if you have the need to do this on Windows without the need of any 3rd party tools such that you can put it in a batch file, Powershell can extract any node from the your pom.xml file like so:



                                                      powershell -Command "& {select-xml //pom:project/pom:properties/pom:mypluginversion -path pom.xml -Namespace  @{pom='http://maven.apache.org/POM/4.0.0'} | foreach {$_.Node.Innerxml}}" > myPluginVersion.txt





                                                      share|improve this answer




























                                                        -1














                                                        I know your question says Linux but if you have the need to do this on Windows without the need of any 3rd party tools such that you can put it in a batch file, Powershell can extract any node from the your pom.xml file like so:



                                                        powershell -Command "& {select-xml //pom:project/pom:properties/pom:mypluginversion -path pom.xml -Namespace  @{pom='http://maven.apache.org/POM/4.0.0'} | foreach {$_.Node.Innerxml}}" > myPluginVersion.txt





                                                        share|improve this answer


























                                                          -1












                                                          -1








                                                          -1







                                                          I know your question says Linux but if you have the need to do this on Windows without the need of any 3rd party tools such that you can put it in a batch file, Powershell can extract any node from the your pom.xml file like so:



                                                          powershell -Command "& {select-xml //pom:project/pom:properties/pom:mypluginversion -path pom.xml -Namespace  @{pom='http://maven.apache.org/POM/4.0.0'} | foreach {$_.Node.Innerxml}}" > myPluginVersion.txt





                                                          share|improve this answer













                                                          I know your question says Linux but if you have the need to do this on Windows without the need of any 3rd party tools such that you can put it in a batch file, Powershell can extract any node from the your pom.xml file like so:



                                                          powershell -Command "& {select-xml //pom:project/pom:properties/pom:mypluginversion -path pom.xml -Namespace  @{pom='http://maven.apache.org/POM/4.0.0'} | foreach {$_.Node.Innerxml}}" > myPluginVersion.txt






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Oct 26 '15 at 21:55









                                                          Peter LubczynskiPeter Lubczynski

                                                          1




                                                          1






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Super User!


                                                              • 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%2fsuperuser.com%2fquestions%2f369996%2fscripting-what-is-the-easiest-to-extract-a-value-in-a-tag-of-a-xml-file%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

                                                              Plaza Victoria

                                                              Puebla de Zaragoza

                                                              Musa