-1

remove the - so it can be read as xml

Solution:

# read XML file
my $filename = 'Finesse.txt';
open $FILE, '<', $filename or die "Can't open file $!";
my $document = do { local $/; <$FILE> };
$document =~ s/^[\- ] //mg; 

my $dom = XML::LibXML->load_xml(string => $document);

say "Users in READY mode:";
say " ";
$teller = 0;

foreach my $title ($dom->findnodes('Team/users/User')) {
$status = $title->findvalue('./state');
if ($status eq "READY") {
    say 'Naam  : ', $title->findvalue('./firstName');
    $teller++;
    }
}
say "Total = ", $teller;

the below file is created from the output of a website

the finesse.txt

- <Team>
  <id>8</id> 
  <name>OurSupport</name> 
  <uri>/finesse/api/Team/8</uri> 
- <users>
- <User>
  <dialogs>/finesse/api/User/C.person/Dialogs</dialogs> 
  <extension /> 
  <firstName>Caz</firstName> 
  <lastName>Person</lastName> 
  <loginId>C.Person</loginId> 
  <pendingState /> 
  <state>ACTIVE</state> 
  <stateChangeTime>2019-07-15T19:54:40.846Z</stateChangeTime> 
  <uri>/finesse/api/User/C.Person</uri> 
  </User>
  </users>
  </Team>

thanks for giving hints for the solution.

  • 1
    What is the actual question you are asking: how to remove the "-" or how to parse XML in perl? Try to limit to one question per post. – Nicola Ambrosetti Jul 18 '19 at 13:25
  • 2
    It is recommended that you do not use [XML::Simple](https://metacpan.org/pod/XML::Simple), please consider using e.g. [XML::LibXML](https://metacpan.org/pod/XML::LibXML) instead. Please explain more clearly what are the purposes of the `-` signs in `finesse.txt`? – Håkon Hægland Jul 18 '19 at 13:27
  • Why is the expected output `C.Person : Active`? Also there are no `READY` strings in the file, what does that mean? – Håkon Hægland Jul 18 '19 at 13:33
  • the - signs in the finesse.txt are produced by a webpage, and I can not change that output, so I have to deal with it. you are right, READY should have been ACTIVE – rene verkerke Jul 18 '19 at 14:17
  • 2
    I recognise this as copy/paste from Firefox default style for XML. Just **save** the browser document, then you don't have +/- collapse markers in it. – daxim Jul 18 '19 at 14:27

1 Answers1

1

That's not a valid XML file. You'll need to fix it. Fortunately, this can be done quite easily.

$xml =~ s/^[\- ] //mg;

By the way, you are using a module whose own documentation recommends against using it. Despite its name, XML::Simple is the most complicated XML parser to use. Please use something else such as XML::LibXML.

ikegami
  • 367,544
  • 15
  • 269
  • 518