title: XML to JSON conversion using perl
body: <p>I am using perl for converting an XML file JSON. I was able to write the code for converting the input XML file to Json format.</p>
  
  <p>Here is the sample xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no" ?&gt;
     &lt;Person SchemaVersion="1.0.8"&gt;
        &lt;ID&gt;0&lt;/ID&gt;
  
        &lt;Home ID="ABC-XYZ" State="Unknown"&gt;
           &lt;Location&gt;
              &lt;SiteName&gt;&lt;/SiteName&gt;
              &lt;Number&gt;62&lt;/Number&gt;
              &lt;MaxSize&gt;0&lt;/MaxSize&gt;
              &lt;Comment&gt;&lt;/Comment&gt;
           &lt;/Location&gt;
  
           &lt;Laptop&gt;
               &lt;FileName&gt;/usr/temp/RPM_020515_.tar.gz&lt;/FileName&gt;
           &lt;/Laptop&gt;
      &lt;/Home&gt;
    &lt;/Person&gt;
  </code></pre>
  
  <p>sample perl code doing json conversion:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use JSON;
  use XML::Simple;
  
  # Create the object of XML Simple
  my $xmlSimple = new XML::Simple(KeepRoot   =&gt; 1);
  
  # Load the xml file in object
  my $dataXML = $xmlSimple-&gt;XMLin("input/path/to/above/XMLFILE");
  
  # use encode json function to convert xml object in json.
  my $jsonString = encode_json($dataXML);
  
  # finally print json
  print $jsonString;
  </code></pre>
  
  <p>Output JSON value:</p>
  
  <pre><code>{
      "Person": {
         "ID": "0", 
         "SchemaVersion": "1.0.8", 
         "Home": {
            "ID": "ABC-XYZ", 
  
            "Laptop": {
               "FileName": "/usr/temp/RPM_020515_.tar.gz"
             }, 
  
            "Location": {
                "Number": "62", 
                "MaxSize": "0", 
                "Comment": { }, 
                "SiteName": { }
             }, 
  
            "State": "Unknown"
         }
     }
  }
  </code></pre>
  
  <p>Above code is working fine.</p>
  
  <p>My question is and this is where i am actually stuck.</p>
  
  <p>I need to do one more thing along with JSON conversion which is checking if element "FileName" in XML is empty or not. if its not extract its value as well.</p>
  
  <pre><code>So output will be two things:
  1. XML To JSON convert ( working fine )
  2. Along with point 1. extract the value of nested element in XML        
     "FileName". I need that for some business logic need in next step.
  </code></pre>
  
  <p>Can some perl experts help me here and suggest me how i can do that in my current perl code.</p>
  
  <p>Thanks for helping in advance. 
  This is my first perl script so please excuse me if this is a too trivial question to ask.</p>
  
  <p>Tried reading the perl docs but not that helpful.</p>
  
  <p>NOTE: I am trying to use only perl built in libraries not any new third party libraries that is the reason i used XML::Simple. Its production code restriction ( very bad boundation ). I hope something for this exists in XML::Simple or JSON.</p>