Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1682 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  [PHP] Comment parser un XML sans racine ?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[PHP] Comment parser un XML sans racine ?

n°2054000
real_matho​s
Posté le 03-02-2011 à 11:41:04  profilanswer
 

Salut,
Je suis en train d'écrire un script qui insère les données contenues dans un fichier BibTex dans une base de données MySQL.
Je convertis d'abord le fichier BibTex en XML à l'aide d'un script python trouvé sur le net, puis je parse le XML obtenu grâce à la classe SimpleXMLElement de PHP.
 
Le problème est que le XML obtenu n'a pas de racine, c'est à dire tous les éléments sont tous directement placés dans l'architecture, sans être placés dans un même élément de DOM.
 
Je crée donc un nouvel objet SimpleXMLElement à partir du code XML obtenu de la conversion BibTex -> XML:

Code :
  1. $xml = new SimpleXMLElement($this->convertedXML, 0, false, 'bibtex', true);


 
Voici ensuite le debug de mon script:
 
$xml->asXML():

Code :
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE bibtex:file PUBLIC "-//BibTeXML//DTD XML for BibTeX v1.0//EN" "bibtexml.dtd">
  3. <bibtex:file xmlns:bibtex="http://bibtexml.sf.net/">
  4.  
  5. <bibtex:entry id="btxdoc">
  6. <bibtex:manual>
  7. <bibtex:author>Oren Patashnik</bibtex:author>
  8. <bibtex:title>BibTeXing</bibtex:title>
  9. <bibtex:address>BibTeX distribution</bibtex:address>
  10. <bibtex:month>February</bibtex:month>
  11. <bibtex:year>1988</bibtex:year>
  12. <bibtex:url>ftp://sunsite.unc.edu/pub/packages/TeX/biblio/bibtex/distribs/doc/btxdoc.tex</bibtex:url>
  13. </bibtex:manual>
  14. </bibtex:entry>
  15. <bibtex:entry id="Tufte-1997-VEI">
  16. <bibtex:book>
  17. <bibtex:author>Edward R. Tufte</bibtex:author>
  18. <bibtex:title>Visual Explanations: Images and Quantities, Evidence and Narrative</bibtex:title>
  19. <bibtex:publisher>Graphics Press</bibtex:publisher>
  20. <bibtex:address>Box 430, Cheshire, CT 06410, USA</bibtex:address>
  21. <bibtex:pages>156</bibtex:pages>
  22. <bibtex:year>1997</bibtex:year>
  23. <bibtex:isbn>0-9613921-2-6</bibtex:isbn>
  24. <bibtex:lccn>P93.5.T846 1997</bibtex:lccn>
  25. <bibtex:mrnumber>B.Tufte.97</bibtex:mrnumber>
  26. <bibtex:bibdate>Thu Aug 07 13:39:07 1997</bibtex:bibdate>
  27. <bibtex:price>US\$45.00</bibtex:price>
  28. <bibtex:acknowledgement>ack-nhfb"(not listed)"</bibtex:acknowledgement>
  29. <bibtex:class>map2</bibtex:class>
  30. <bibtex:keywords>scglib</bibtex:keywords>
  31. </bibtex:book>
  32. </bibtex:entry>
  33. <bibtex:entry id="robinson">
  34. <bibtex:book>
  35. <bibtex:editor>Robinson, W. F. and Huxtable, C. R. R.</bibtex:editor>
  36. <bibtex:title>Clinicopathologic Principles For Veterinary Medicine</bibtex:title>
  37. <bibtex:publisher>Cambridge University Press</bibtex:publisher>
  38. <bibtex:year>1988</bibtex:year>
  39. <bibtex:address>Cambridge</bibtex:address>
  40. </bibtex:book>
  41. </bibtex:entry>
  42. <bibtex:entry id="huffman">
  43. <bibtex:article>
  44. <bibtex:author>Huffman, L. M.</bibtex:author>
  45. <bibtex:title>Processing whey protein for use as a food ingredient</bibtex:title>
  46. <bibtex:journal>Food Technology</bibtex:journal>
  47. <bibtex:year>1996</bibtex:year>
  48. <bibtex:volume>50</bibtex:volume>
  49. <bibtex:number>2</bibtex:number>
  50. <bibtex:pages>49-52</bibtex:pages>
  51. </bibtex:article>
  52. </bibtex:entry>
  53. <bibtex:entry id="graps">
  54. <bibtex:article>
  55. <bibtex:author>Graps, Amara L.</bibtex:author>
  56. <bibtex:title>An Introduction to Wavelets</bibtex:title>
  57. <bibtex:journal>IEEE Computational Sciences and Engineering</bibtex:journal>
  58. <bibtex:year>1995</bibtex:year>
  59. <bibtex:volume>2</bibtex:volume>
  60. <bibtex:number>2</bibtex:number>
  61. <bibtex:pages>50-61</bibtex:pages>
  62. <bibtex:note>http://www.amara.com/ftpstuff/IEEEwavelet.ps.gz</bibtex:note>
  63. </bibtex:article>
  64. </bibtex:entry>
  65. <bibtex:entry id="berkman">
  66. <bibtex:book>
  67. <bibtex:author>Berkman, R. I.</bibtex:author>
  68. <bibtex:title>Find It Fast: How to Uncover Expert Information on Any Subject</bibtex:title>
  69. <bibtex:publisher>HarperPerennial</bibtex:publisher>
  70. <bibtex:year>1994</bibtex:year>
  71. <bibtex:address>New York</bibtex:address>
  72. </bibtex:book>
  73. </bibtex:entry>
  74. <bibtex:entry id="chicago">
  75. <bibtex:inbook>
  76. <bibtex:title>The Chicago Manual of Style</bibtex:title>
  77. <bibtex:publisher>University of Chicago Press</bibtex:publisher>
  78. <bibtex:edition>Thirteenth</bibtex:edition>
  79. <bibtex:year>1982</bibtex:year>
  80. <bibtex:pages>400-401</bibtex:pages>
  81. <bibtex:key>Chicago</bibtex:key>
  82. </bibtex:inbook>
  83. </bibtex:entry>
  84.  
  85. <bibtex:entry id="testcase-percent">
  86. <bibtex:article>
  87. <bibtex:author>Doe, J.</bibtex:author>
  88. <bibtex:title>The use of \% gives trouble</bibtex:title>
  89. <bibtex:journal>Doe's Journal</bibtex:journal>
  90. <bibtex:year>2004</bibtex:year>
  91. <bibtex:volume>1</bibtex:volume>
  92. <bibtex:number>1</bibtex:number>
  93. <bibtex:pages>1-2</bibtex:pages>
  94. </bibtex:article>
  95. </bibtex:entry>
  96. <bibtex:entry id="Gawinecki2005a">
  97. <bibtex:inproceedings>
  98. <bibtex:author>
  99. <bibtex:person>Maciej Gawinecki</bibtex:person>
  100. <bibtex:person>Minor Gordon</bibtex:person>
  101. <bibtex:person>Marcin Paprzycki</bibtex:person>
  102. <bibtex:person>Michał Szymczak</bibtex:person>
  103. <bibtex:person>Zygmunt Vetulani</bibtex:person>
  104. <bibtex:person>Jimmy Wright</bibtex:person>
  105. </bibtex:author>
  106. <bibtex:title>Enabling Semantic Referencing of Selected Travel Related Resources</bibtex:title>
  107. <bibtex:booktitle>Proceedings of the 8 th International Conference on Business Information Systems (BIS 2005)</bibtex:booktitle>
  108. <bibtex:year>2005</bibtex:year>
  109. <bibtex:pages>271-28</bibtex:pages>
  110. <bibtex:address>Poznań, Poland</bibtex:address>
  111. <bibtex:publisher>Poznań University of Economics Press</bibtex:publisher>
  112. <bibtex:abstract>This paper describes our efforts to semantically describe travelrelated documents on the Internet. We focus on the areas of lodging and gastronomy, with allowance for further expansion into other aspects of the world of travel. Based on an analysis of currently-available data, we develop hotel ontology. Furthermore, we explicitly define an ontology for the Chefmoz project data, which has no official schema. Finally, we sketch a way to combine these two ontologies for use in an integrated travel framework.</bibtex:abstract>
  113. <bibtex:owner>maciej</bibtex:owner>
  114. <bibtex:timestamp>2006.07.13</bibtex:timestamp>
  115. <bibtex:url>http://agentlab.swps.edu.pl/BIS_2005.pdf</bibtex:url>
  116. </bibtex:inproceedings>
  117. </bibtex:entry>
  118.   <!-- manual cleanup may be required... -->
  119. </bibtex:file>


 
$xml->children('bibtex', true)->asXML() :

Code :
  1. <bibtex:entry id="btxdoc">
  2. <bibtex:manual>
  3. <bibtex:author>Oren Patashnik</bibtex:author>
  4. <bibtex:title>BibTeXing</bibtex:title>
  5. <bibtex:address>BibTeX distribution</bibtex:address>
  6. <bibtex:month>February</bibtex:month>
  7. <bibtex:year>1988</bibtex:year>
  8. <bibtex:url>ftp://sunsite.unc.edu/pub/packages/TeX/biblio/bibtex/distribs/doc/btxdoc.tex</bibtex:url>
  9. </bibtex:manual>
  10. </bibtex:entry>


 
$xml->count() :

Citation :

9


 
$xml->attributes('bibtex', true)->asXML() :

Citation :


 


 
Donc le problème est que je n'arrive à récupérer que le premier élément de la liste, et je n'arrive pas à saisir comment je dois procéder sans "racine" pour encadrer mes éléments...
 
Quelqu'un peut m'aider à résoudre ce problème ?
 
Merci d'avance!


Message édité par real_mathos le 03-02-2011 à 11:43:02
mood
Publicité
Posté le 03-02-2011 à 11:41:04  profilanswer
 

n°2054005
gilou
Modérateur
Modzilla
Posté le 03-02-2011 à 12:00:55  profilanswer
 

1) La liste n'est pas plate, puisqu'on a un noeud racine bibtex:file et une liste de 9 noeuds fils bibtex:entry.
2) la moindre recherche google donne immédiatement la méthode a employer, alors désolé, mais faudrait peut être pas regarder dans sa paume pousser le poil.
La doc: http://www.php.net/manual/fr/simpl [...] ildren.php exemple 1
foreach ($xml->children() as ...) {  }
A+,


Message édité par gilou le 03-02-2011 à 12:19:27

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  [PHP] Comment parser un XML sans racine ?

 

Sujets relatifs
Parser un fichier BibTex pour l'insérer dans une bdd MySQL[PHP] Jointure sur plusieurs champ ?
[PHP] quelle librairie pour faire des graphes type candlestick?Erreur d'analyse XML
executer commandes root avec PHP![PHP]Envoyer une fichier à un utilisateur sans lui donner l'adresse
Script PHP d'un éditeur de RPG[PHP] fournir un fichier hors document root
Problème d'affiche en PHP[PHP] arry_unique multidimentional + index d'array
Plus de sujets relatifs à : [PHP] Comment parser un XML sans racine ?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR