Domanda

Sono un principiante perl che tenta di leggere un file SGML, analizzalo, quindi convertilo in XML in modo da poter ottenere le coppie chiave/valore di tutti gli elementi. Ho trovato il SGML :: dtdparse e XML :: Simple moduli come penso che questo sia quello che voglio per l'attività. Il mio problema è che non riesco a trovare alcuna documentazione su dtdparse o esempi di codice.

Il mio codice è di seguito:

# use modules
use SGML::DTDParse;
use XML::Simple;
use Data::Dumper;

use warnings;
use strict;

my $xml;
my $data;
my $convert;

$/ = undef;
open FILE, "C:/..." or die $!;
my $file = <FILE>;

# Convert the DTD file to XML
dtdParse $file;

# Create the XML object
$xml = new XML::Simple;

# Read the XML file
$data = $xml->XMLin($file);

# print the output
print Dumper($data);

Ricevo un errore con la riga di file dtdparse $ come segue: Impossibile chiamare il metodo "dtdparse" senza un pacchetto o un riferimento oggetto a "il mio nome script"

Qualche idea sulla sintassi corretta qui ed è un approccio valido per l'attività?

Ho rielaborato di nuovo il codice il codice e sono stato in grado di fare l'analisi del DTD con questo:

$dtd = SGML::DTDParse::DTD->new();
$dtd->parse($file);
print $dtd;

Tuttavia, non credo che il file analizzato possa essere considerato XML, quindi forse il modo corretto per ottenere tutti gli elementi dal file analizzato è un ciclo per.

È stato utile?

Soluzione

Non esiste una funzione dtdparse.

DTDPASSE è un programma in arrivo con il modulo SGML :: DTDParse.

È possibile usarlo per scaricare XML da un file DTD. Un rapido esempio di come si potrebbe usare dtdparse:

use strict;
use warnings;

use SGML::DTDParse;
use XML::Simple;
use Data::Dumper;

# Convert the DTD file to XML
my $result = qx{dtdparse test.dtd};

# Create the XML object
my $xml = new XML::Simple;

# Read the XML file
$result = $xml->XMLin($result);

# print the output
$Data::Dumper::Indent = 1;
print Dumper($result);

dove test.dtd sembra questo:

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT DatabaseInventory (DatabaseName+)>
<!ELEMENT DatabaseName (   GlobalDatabaseName
                         , OracleSID
                         , DatabaseDomain
                         , Administrator+
                         , DatabaseAttributes
                         , Comments)
>
<!ELEMENT GlobalDatabaseName (#PCDATA)>
<!ELEMENT OracleSID          (#PCDATA)>
<!ELEMENT DatabaseDomain     (#PCDATA)>
<!ELEMENT Administrator      (#PCDATA)>
<!ELEMENT DatabaseAttributes EMPTY>
<!ELEMENT Comments           (#PCDATA)>

<!ATTLIST Administrator       EmailAlias CDATA #REQUIRED>
<!ATTLIST Administrator       Extension  CDATA #IMPLIED>
<!ATTLIST DatabaseAttributes  Type       (Production|Development|Testing) #REQUIRED>
<!ATTLIST DatabaseAttributes  Version    (7|8|8i|9i) "9i">

<!ENTITY AUTHOR "Jeffrey Hunter">
<!ENTITY WEB    "www.iDevelopment.info">
<!ENTITY EMAIL  "jhunter@iDevelopment.info">

Il che emetterà qualcosa di simile:

$VAR1 = {
  'namecase-entity' => '0',
  'created-by' => 'DTDParse V2.00',
  'public-id' => '',
  'version' => '1.0',
  'attlist' => {
    'DatabaseAttributes' => {
      'attribute' => {
        'Type' => {
          'value' => 'Production Development Testing',
          'type' => '#REQUIRED',
          'default' => '',
          'enumeration' => 'yes'
        },
        'Version' => {
          'value' => '7 8 8i 9i',
          'type' => '',
          'default' => '9i',
          'enumeration' => 'yes'
        }
      },
      'attdecl' => '  Type       (Production|Development|Testing) #REQUIRED'
    },
    'Administrator' => {
      'attribute' => {
        'EmailAlias' => {
          'value' => 'CDATA',
          'type' => '#REQUIRED',
          'default' => ''
        },
        'Extension' => {
          'value' => 'CDATA',
          'type' => '#IMPLIED',
          'default' => ''
        }
      },
      'attdecl' => '       EmailAlias CDATA #REQUIRED'
    }
  },
  'element' => {
    'OracleSID' => {
      'content-type' => 'mixed',
      'content-model-expanded' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      },
      'content-model' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      }
    },
    'Comments' => {
      'content-type' => 'mixed',
      'content-model-expanded' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      },
      'content-model' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      }
    },
    'DatabaseAttributes' => {
      'content-type' => 'element',
      'content-model-expanded' => {
        'empty' => {}
      },
      'content-model' => {
        'empty' => {}
      }
    },
    'GlobalDatabaseName' => {
      'content-type' => 'mixed',
      'content-model-expanded' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      },
      'content-model' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      }
    },
    'Administrator' => {
      'content-type' => 'mixed',
      'content-model-expanded' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      },
      'content-model' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      }
    },
    'DatabaseInventory' => {
      'content-type' => 'element',
      'content-model-expanded' => {
        'sequence-group' => {
          'element-name' => {
            'occurrence' => '+',
            'name' => 'DatabaseName'
          }
        }
      },
      'content-model' => {
        'sequence-group' => {
          'element-name' => {
            'occurrence' => '+',
            'name' => 'DatabaseName'
          }
        }
      }
    },
    'DatabaseDomain' => {
      'content-type' => 'mixed',
      'content-model-expanded' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      },
      'content-model' => {
        'sequence-group' => {
          'pcdata' => {}
        }
      }
    },
    'DatabaseName' => {
      'content-type' => 'element',
      'content-model-expanded' => {
        'sequence-group' => {
          'element-name' => {
            'Comments' => {},
            'OracleSID' => {},
            'DatabaseAttributes' => {},
            'DatabaseDomain' => {},
            'GlobalDatabaseName' => {},
            'Administrator' => {
              'occurrence' => '+'
            }
          }
        }
      },
      'content-model' => {
        'sequence-group' => {
          'element-name' => {
            'Comments' => {},
            'OracleSID' => {},
            'DatabaseAttributes' => {},
            'DatabaseDomain' => {},
            'GlobalDatabaseName' => {},
            'Administrator' => {
              'occurrence' => '+'
            }
          }
        }
      }
    }
  },
  'entity' => {
    'WEB' => {
      'text-expanded' => 'www.iDevelopment.info',
      'text' => 'www.iDevelopment.info',
      'type' => 'gen'
    },
    'AUTHOR' => {
      'text-expanded' => 'Jeffrey Hunter',
      'text' => 'Jeffrey Hunter',
      'type' => 'gen'
    },
    'EMAIL' => {
      'text-expanded' => 'jhunter@iDevelopment.info',
      'text' => 'jhunter@iDevelopment.info',
      'type' => 'gen'
    }
  },
  'system-id' => 'test.dtd',
  'unexpanded' => '1',
  'created-on' => 'Tue Feb 28 00:44:52 2012',
  'declaration' => '',
  'xml' => '0',
  'title' => '?untitled?',
  'namecase-general' => '1'
};

Altri suggerimenti

dtdparse non è una funzione perl; È uno script per l'elaborazione di un DTD SGML dalla riga di comando. La documentazione per lo script è qui.

Dato che vuoi fare l'analisi nel tuo script perl, puoi usare la fonte di dtdparse come esempio se vuoi.

Per SGML, usa SP di James Clark, che include un Convertitore da SGML a XML chiamato SX. Questo è un sistema professionale e ha una documentazione. Se hai bisogno di perl lì dentro, usa system o open Per chiamare SP/SX come programma esterno.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top