πŸ“œ ⬆️ ⬇️

SimpleXML. Extended Usage Example

I think many users are already familiar with this wonderful class for processing xml documents. In this article I want to give a few examples of use that I did not find in the official documentation , but which would be useful for novice users.

Simple example of use


$ xml = <<< EOF
<? xml version = "1.0" encoding = "UTF-8" ? >
< recipe name = β€œbread” preptime = β€œ5” cooktime = β€œ180” >
< title > Simple Bread </ title >
< ingredient amount = "3" unit = "glass" > Flour </ ingredient >
< ingredient amount = "0.25" unit = "gram" > Yeast </ ingredient >
< ingredient amount = "1.5" unit = "glass" > Warm water </ ingredient >
< ingredient amount = "1" unit = "teaspoon" > Salt </ ingredient >
< Instructions >
< step > Mix all ingredients and knead thoroughly. </ step >
< step > Cover with a cloth and leave for one hour in a warm room. </ step >
< step > Knead again, put on a baking sheet and put in the oven. </ step >
</ Instructions >
</ recipe >
EOF;
$ sx = new SimpleXMLElement ($ xml);
var_dump ($ sx); * This source code was highlighted with Source Code Highlighter .

This code will output the following result to the browser:
object ( SimpleXMLElement ) [ 1 ]
public '@attributes' =>
array
'name' => string 'bread' (length = 8)
'preptime' => string '5' (length = 1)
'cooktime' => string '180' (length = 3)
public 'title' => string 'Simple bread' (length = 23)
public 'ingredient' =>
array
0 => string 'Flour' (length = 8)
1 => string 'Yeast' (length = 12)
2 => string 'Warm Water' (length = 21)
3 => string 'Salt' (length = 8)
public 'Instructions' =>
object ( SimpleXMLElement ) [ 2 ]
public 'step' =>
array
0 => string 'Mix all ingredients and knead thoroughly.' (length = 84)
1 => string 'Close with a cloth and leave for one hour in a warm room.' (length = 104)
2 => string 'Knead again, put on a baking sheet and put in the oven.' (length = 113)

As you can see, everything is quite transparent and no documentation is needed. But what will happen if you feed this class:

Difficult example


This is a Microsoft Excel 2003 XML document.
<? xml version = "1.0" encoding = "UTF-8" ? > <? mso-application progid = "Excel.Sheet" ? >
< Workbook xmlns = "urn: schemas-microsoft-com: office: spreadsheet" xmlns: xsi = " www.w3.org/2001/XMLSchema-instance " xmlns: x = "urn: schemas-microsoft-com: office: excel " Xmlns: x2 =" schemas.microsoft.com/office/excel/2003/xml " xmlns: ss =" urn: schemas-microsoft-com: office: spreadsheet " xmlns: o =" urn: schemas-microsoft-com: office: office " xmlns: html =" www.w3.org/TR/REC-html40 " xmlns: c =" urn: schemas-microsoft-com: office: component: spreadsheet " >
< OfficeDocumentSettings xmlns = "urn: schemas-microsoft-com: office: office" >
< Colors >
< Color >
< Index > 3 </ Index >
< RGB > # c0c0c0 </ RGB >
</ Color >
< Color >
< Index > 4 </ Index >
< RGB > # ff0000 </ RGB >
</ Color >
</ Colors >
</ OfficeDocumentSettings >
< ExcelWorkbook xmlns = "urn: schemas-microsoft-com: office: excel" >
< WindowHeight > 9000 </ WindowHeight >
< WindowWidth > 13860 </ WindowWidth >
< WindowTopX > 240 </ WindowTopX >
< WindowTopY > 75 </ WindowTopY >
< ProtectStructure > False </ ProtectStructure >
< ProtectWindows > False </ ProtectWindows >
</ ExcelWorkbook >
< Styles >
< Style ss: ID = "Default" ss: Name = "Default" />
< Style ss: ID = "Result" ss: Name = "Result" >
< Font ss: Bold = "1" ss: Italic = "1" ss: Underline = "Single" />
</ Style >
< Style ss: ID = "Result2" ss: Name = "Result2" >
< Font ss: Bold = "1" ss: Italic = "1" ss: Underline = "Single" />
< NumberFormat ss: Format = "Currency" />
</ Style >
< Style ss: ID = "Heading" ss: Name = "Heading" >
< Alignment ss: Horizontal = "Center" />
< Font ss: Bold = "1" ss: Italic = "1" ss: Size = "16" />
</ Style >
< Style ss: ID = "Heading1" ss: Name = "Heading1" >
< Alignment ss: Horizontal = "Center" ss: Rotate = "90" />
< Font ss: Bold = "1" ss: Italic = "1" ss: Size = "16" />
</ Style >
< Style ss: ID = "co1" />
< Style ss: ID = "ta1" />
</ Styles >
< ss: Worksheet ss: Name = "Sheet1" >
< Table ss: StyleID = "ta1" >
< Column ss: Span = "2" ss: Width = "64.2614" />
< Row ss: Height = "12.1039" >
< Cell >
< Data ss: Type = "Number" > 1 </ Data >
</ Cell >
< Cell >
< Data ss: Type = "Number" > 2 </ Data >
</ Cell >
< Cell >
< Data ss: Type = "Number" > 3 </ Data >
</ Cell >
</ Row >
< Row ss: Height = "12.1039" >
< Cell >
< Data ss: Type = "Number" > 2 </ Data >
</ Cell >
< Cell ss: Formula = "= R [-1] C * RC1" >
< Data ss: Type = "Number" > 4 </ Data >
</ Cell >
< Cell ss: Formula = "= R [-1] C * RC1" >
< Data ss: Type = "Number" > 6 </ Data >
</ Cell >
</ Row >
< Row ss: Height = "12.1039" >
< Cell >
< Data ss: Type = "Number" > 3 </ Data >
</ Cell >
< Cell ss: Formula = "= R [-2] C * RC1" >
< Data ss: Type = "Number" > 6 </ Data >
</ Cell >
< Cell ss: Formula = "= R [-2] C * RC1" >
< Data ss: Type = "Number" > 9 </ Data >
</ Cell >
</ Row >
</ Table >
< x: WorksheetOptions />
</ ss: Worksheet >
< ss: Worksheet ss: Name = "Sheet2" >
< Table ss: StyleID = "ta1" >
< Column ss: Width = "64.2614" />
< Row ss: Height = "12.1039" >
< Cell ss: Index = "1" />
</ Row >
</ Table >
< x: WorksheetOptions />
</ ss: Worksheet >
< ss: Worksheet ss: Name = "Sheet3" >
< Table ss: StyleID = "ta1" >
< Column ss: Width = "64.2614" />
< Row ss: Height = "12.1039" >
< Cell ss: Index = "1" />
</ Row >
</ Table >
< x: WorksheetOptions />
</ ss: Worksheet >
</ Workbook > * This code was highlighted with Source Code Highlighter .

Now our code will not display all the information about the document.
I will not give a complete conclusion. Let me just say that there are no ss: Worksheet nodes in the output.

Getting the ss: Worksheet node
Method one. Xpath

foreach ($ sx-> xpath ('// ss: Worksheet') as $ x) print_r ($ x);

The second way. NS

The document defines the following ns
xmlns = "urn: schemas-microsoft-com: office: spreads"
xmlns: xsi = " www.w3.org/2001/XMLSchema-instance "
xmlns: x = "urn: schemas-microsoft-com: office: excel"
xmlns: x2 = " schemas.microsoft.com/office/excel/2003/xml "
xmlns: ss = "urn: schemas-microsoft-com: office: spreads"
xmlns: o = "urn: schemas-microsoft-com: office: office"
xmlns: html = " www.w3.org/TR/REC-html40 "
xmlns: c = "urn: schemas-microsoft-com: office: component: spreads"

To access the Worksheet, which is defined in urn: schemas-microsoft-com: office: spreadsheet, you will need the following code
$ sx-> children ('urn: schemas-microsoft-com: office: spreadsheet') -> Worksheet

To access the attributes, you also need to explicitly specify ns.
')
Ps. Some difficulties are also added by the fact that in SimpleXML most of the methods have iterative properties and it is not always possible to use the functions var_dump, print_r, etc ...
PSPS: Perhaps this note will be useless for you, but having encountered the problem, rummaged through quite a lot of information, there were no examples ...

Source: https://habr.com/ru/post/37000/


All Articles