PHP 像文件一样读取和解析 XML

PHP Read and Parse XML like File

我们正在用 PHP 索引我们的期刊。我们有日志元数据文件。我正在尝试使用 PHP SimpleXML 来解析它,但我遇到了很多错误。

Warning: simplexml_load_string(): Entity: line 19: parser error : Opening and ending tag mismatch: XUI line 19 and BB in *** on line 62

Warning: simplexml_load_string(): s;S PERSPECTIVE

Warning: simplexml_load_string(): ^ in *** on line 62

Warning: simplexml_load_string(): Entity: line 44: parser error : Opening and ending tag mismatch: BB line 4 and D in *** on line 62

Warning: simplexml_load_string(): 33rd ed. St. Louis, MO: Elsevier Health Sciences; 2016.

Warning: simplexml_load_string(): ^ in *** on line 62

Warning: simplexml_load_string(): Entity: line 61: parser error : Opening and ending tag mismatch: XUI line 61 and BB in *** on line 62

Warning: simplexml_load_string(): R TO THE EDITOR

Warning: simplexml_load_string(): ^ in *** on line 62

Warning: simplexml_load_string(): Entity: line 74: parser error : Opening and ending tag mismatch: BB line 46 and D in *** on line 62

当我查看文件时,它看起来像一个 XML 文件。我如何用 PHP 解析它?

我使用的代码是:

$file = file_get_contents('xyz.0');

$file = utf8_decode($file);
$file = str_replace("&", "", $file); //For problems with & character

//libxml_use_internal_errors(true);
$xml = simplexml_load_string($file, 'SimpleXMLElement', LIBXML_NOCDATA);

示例XML 来自文件的代码:

<!DOCTYPE dg SYSTEM "ovidbase.dtd"> <DG><COVER NAME="G1893697-201804000-00000"> <D AN="01893697-201804000-00001" V="2009.2F" FILE="G1893697-201804000-00001"> <BB> <TG> <TI>Oh Blood Pressure Measurements&mdash;Where Art Thou&quest;</TI></TG> <BY> <PN><FN>G.</FN><MN>Stephen</MN><SN>Morris</SN><DEG>PT, PhD, FACSM</DEG></PN> <AF><P>President, Oncology Section of the APTA; and Professor, Department of Physical Therapy, Wingate University, Wingate, NC</P></AF> <BT><P><E T="B">Correspondence:</E> G. Stephen Morris, PT, PhD, FACSM, Department of Physical Therapy, Wingate University, 215 N. Camden Rd, Wingate, NC 28174 (<URL>s.morris&commat;wingate.edu</URL>).</P><P>The author declares no conflicts of interest.</P></BT></BY> <SO> <PB>Rehabilitation Oncology</PB> <ISN>2168-3808</ISN> <DA><MO>April</MO><YR>2018</YR></DA> <V>36</V> <IS><IP>2</IP></IS> <PG>79&ndash;80</PG></SO> <CP>&copy; 2018 Oncology Section, APTA.</CP> <DT>PRESIDENT&apos;S PERSPECTIVE</DT><XUI XDB="pub-doi" UI="10.1097/01.REO.0000000000000118"></BB> <BD> <LV1><HD>&NA;</HD> <P>physical therapy&quest;</P></LV1> <LV1><SG><SGN>G. Stephen Morris, PT, PhD, FACSM</SGN></SG></LV1></BD> <ED> <EDS><HD>REFERENCES</HD> <RF ID="R1-1">1. <JRF><DRF>Arena SK, Reyes A, Rolf M. Behaviors, and knowledge of outpatient physical therapists. Cardiopulm Phys Ther J. 2018;9:3&ndash;12.</DRF><PN><FN>SK</FN><SN>Arena</SN></PN><PN><FN>A</FN><SN>Reyes</SN></PN><PN><FN>M</FN><SN>Rolf</SN></PN><TI>Behaviors, and knowledge of outpatient physical therapists</TI><PB>Cardiopulm Phys Ther J</PB><DA><YR>2018</YR></DA><V>9</V><PG>3&ndash;12</PG></JRF></RF> <RF ID="R2-1">2. <URF>US Preventative Services Task Force. High blood pressure in adults: screening. https:&sol;&sol;www.uspreventiveservicestaskforce.org&sol;Page&sol;Document&sol;RecommendationStatementFinal&sol;high-blood-pressure-in-adults-screening. Accessed January 12, 2018.</URF></RF> <RF ID="R3-1">3. <URF>Centers for Disease Control and Prevention. High blood pressure fact sheet. https:&sol;&sol;www.cdc.gov&sol;bloodpressure&sol;facts.htm. Accessed January 12, 2018.</URF></RF> <RF ID="R4-1">4. <JRF><DRF>Lein DH Jr, Clark D, Graham C, Perez P, Morris D. A model to integrate health promotion and wellness in physical therapist practice: development and validation. Phys Ther. 2017;97(12):1169&ndash;1181.</DRF><PN><FN>DH</FN><SN>Lein</SN></PN><PN><FN>D</FN><SN>Clark</SN></PN><PN><FN>C</FN><SN>Graham</SN></PN><PN><FN>P</FN><SN>Perez</SN></PN><PN><FN>D</FN><SN>Morris</SN></PN><TI>A model to integrate health promotion and wellness in physical therapist practice: development and validation</TI><PB>Phys Ther</PB><DA><YR>2017</YR></DA><V>97</V><PG>1169&ndash;1181</PG></JRF></RF> <RF ID="R5-1">5. <URF>Riebe D, ed. ACSM&apos;s Guidelines for Exercise Testing and Prescription. 10th ed. Baltimore, Maryland: Wolters Kluwer; 2018.</URF></RF> <RF ID="R6-1">6. <JRF><DRF>Pickering TG, Hall JE, Appel LJ, et al Recommendations for blood pressure measurement in humans and experimental animals: part 1: blood pressure measurement in humans: a statement for professionals from the Subcommittee of Professional and Public Education of the American Heart Association Council on High Blood Pressure Research. Circulation. 2005;111(5):697&ndash;716.</DRF><PN><FN>TG</FN><SN>Pickering</SN></PN><PN><FN>JE</FN><SN>Hall</SN></PN><PN><FN>LJ</FN><SN>Appel</SN></PN><TI>Recommendations for blood pressure measurement in humans and experimental animals: part 1: blood pressure measurement in humans: a statement for professionals from the Subcommittee of Professional and Public Education of the American Heart Association Council on High Blood Pressure Research</TI><PB>Circulation</PB><DA><YR>2005</YR></DA><V>111</V><PG>697&ndash;716</PG></JRF></RF> <RF ID="R7-1">7. <JRF><DRF>Rabbia F, Testa E, Rabbia S, et al Effectiveness of blood pressure educational and evaluation program for the improvement of measurement accuracy among nurses. High Blood Press Cardiovasc Prev. 2013;20(2):77&ndash;80.</DRF><PN><FN>F</FN><SN>Rabbia</SN></PN><PN><FN>E</FN><SN>Testa</SN></PN><PN><FN>S</FN><SN>Rabbia</SN></PN><TI>Effectiveness of blood pressure educational and evaluation program for the improvement of measurement accuracy among nurses</TI><PB>High Blood Press Cardiovasc Prev</PB><DA><YR>2013</YR></DA><V>20</V><PG>77&ndash;80</PG></JRF></RF> <RF ID="R8-1">8. <JRF><DRF>Frese EM, Richter RR, Burlis TV. Self-reported measurement of heart rate and blood pressure in patients by physical therapy clinical instructors. Phys Ther. 2002;82(12):1192&ndash;1200.</DRF><PN><FN>EM</FN><SN>Frese</SN></PN><PN><FN>RR</FN><SN>Richter</SN></PN><PN><FN>TV</FN><SN>Burlis</SN></PN><TI>Self-reported measurement of heart rate and blood pressure in patients by physical therapy clinical instructors</TI><PB>Phys Ther</PB><DA><YR>2002</YR></DA><V>82</V><PG>1192&ndash;1200</PG></JRF></RF> <RF ID="R9-1">9. <JRF><DRF>Mouhavar E, Salahudeen A, Yeh ETH. Hypertension in cancer patients. Tex Heart Inst J. 2011;38(3):263&ndash;265.</DRF><PN><FN>E</FN><SN>Mouhavar</SN></PN><PN><FN>A</FN><SN>Salahudeen</SN></PN><PN><FN>ETH</FN><SN>Yeh</SN></PN><TI>Hypertension in cancer patients</TI><PB>Tex Heart Inst J</PB><DA><YR>2011</YR></DA><V>38</V><PG>263&ndash;265</PG></JRF></RF> <RF ID="R10-1">10. <URF>Gahart BL, Nazareno AR, eds. Intravenous Medications: A Handbook for Nurses and Health Professionals. 33rd ed. St. Louis, MO: Elsevier Health Sciences;
2016.</URF></RF></EDS></ED></D> <D AN="01893697-201804000-00002" V="2009.2F" FILE="G1893697-201804000-00002"> <BB> <TG> <TI>In 2018 &ldquo;Spring Is the Time of Plans and Projects&rdquo;</TI></TG> <BY> <PN><FN>Lucinda</FN><MN>(Cindy)</MN><SN>Pfalzer</SN><DEG>PT, PhD, FACSM, FAPTA</DEG></PN> <AF><P>Editor of <E T="I">Oncology Rehabilitation</E> and Emeriti Professor, Physical Therapy Department, University of Michigan-Flint, Flint, MI</P></AF> <BT><P><E T="B">Correspondence:</E> Lucinda (Cindy) Pfalzer, PT, PhD, FACSM, FAPTA, Physical Therapy Department, University of Michigan-Flint, 2157 WSW Bldg, Flint, MI 48502 (<URL>cpfalzer&commat;umich.edu</URL>).</P><P>The author declares no conflicts of interest.</P></BT></BY> <SO> <PB>Rehabilitation Oncology</PB> <ISN>2168-3808</ISN> <DA><MO>April</MO><YR>2018</YR></DA> <V>36</V> <IS><IP>2</IP></IS> <PG>81&ndash;82</PG></SO> <CP>&copy; 2018 Oncology Section, APTA.</CP> <DT>LETTER TO THE EDITOR</DT><XUI XDB="pub-doi" UI="10.1097/01.REO.0000000000000119"></BB> <BD>

您可以从 here 下载 xml 文件。

谢谢

编辑:这与问题 XML parser error: entity not defined 不同。此文件是几年前生成的(2000 年代等)。我没有生成这些文件,我只是尝试解析它们并获取元数据。

编辑 2:抱歉,我也在尝试使用 Dom 解析器进行解析,并在创建 post 时添加了其中的错误。现在我添加了 SimpleXML 错误。

该文件不符合 XML 规范,有一些东西,例如未知实体和非闭合标签。

& 替换为 space 将设法忽略实体,以解决其他一些问题,这是使用正则表达式整理标签的情况(我是不是正则表达式专家,但替换需要 <COVER ...> 并将其转换为 <COVER ... />)...

$file = file_get_contents('20180400.xml');

$file = str_replace("&", "", $file); //For problems with & character
$file = preg_replace('/<COVER (.*?)>/', '<COVER  />', $file);
$file = preg_replace('/<XUI (.*?)>/', '<COVER  />', $file);
$file = preg_replace('/<TGP (.*?)>/', '<COVER  />', $file);

// libxml_use_internal_errors(true);
$xml = simplexml_load_string($file, 'SimpleXMLElement', LIBXML_NOCDATA);
echo $xml->asXML("out.xml");