Skip to content
Advertisement

How to display the attributes of an XML file to a website?

I want to display the data of an XML file to my website, I have a problem for displaying that, because XML file is a bit different. I have to dispaly the value of certain attributes of this XML file. the path of this XML file is stored in side the database. the data which should be displayed I marked as bold(inside double star). Thank you all so much for helping me. I need an example please.

here the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<GO2cam_to_PPU>
    <Header LogName="steff" HostName="PC-STEFF" Date="2021-05-10" Hour="10-06-10"/>
    <Software Version="6009" Revision="200">
        <OEM Number="10" **Name="DEMO Disk 4X"** PremilledMode="No" MountingType="None"/>
        <Protection HostId="192.168.0.203" Type="Floating"/>
    </Software>
    <Project>
        <Machine Name="SBO-GO2cam_Disk_4X.MCF" PostProcessor="F50_Fanuc" NcFile="SBO.NC"/>
        <Stock Number="1">
            <Material **Name="Zirconia.mtr"** Class="Zirconia">
                <Dimension Diameter="94" Width="93.9804412498" **Thickness="26"**/>
                <Shoulder Define="No"/>
                <Teeth Number="1">
                    <Tooth Typology="anatomicCrown">
                        <STL DirectoryName="U:CAD InterfacesSTLCoping - CrownCrown Single" **FileName="0016  ADVANCED KELLY B1_0.STL"** FacetNumber="42782"/>
                        <Opelist DirectoryName="Milling CrownTools - Ball-D2.0-D1.0-D0.6" *FileName="2 - Zirconia - Crown - D2-D1-D0.6 - Standard.OPL"*/>
                    </Tooth>
                </Teeth>
            </Material>
        </Stock>
        <Machining Number="1">
            <Cycle Name="Roughing D2 at 0" Type="411" ToolName="Zr Ball End Mill D2.F14" ToolPosition="1"/>
        </Machining>
        <ToolList Number="1">
            <Tool Name="Zr Ball End Mill D2.F14" Type="460" Diameter="2" Radius="1"/>
        </ToolList>
    </Project>
</GO2cam_to_PPU>

Advertisement

Answer

To accomplish the goal of displaying various Attributes from some XML data using PHP the most common method would be to use DOMDocument and DOMXPath

The following uses an array to aid processing the various queries needed. This is just the way I chose to approach the issue – not necessarily how one must do it.

<?php
    
    $strxml='<?xml version="1.0" encoding="UTF-8"?>
    <GO2cam_to_PPU>
        <Header LogName="steff" HostName="PC-STEFF" Date="2021-05-10" Hour="10-06-10"/>
        <Software Version="6009" Revision="200">
            <OEM Number="10" Name="DEMO Disk 4X" PremilledMode="No" MountingType="None"/>
            <Protection HostId="192.168.0.203" Type="Floating"/>
        </Software>
        <Project>
            <Machine Name="SBO-GO2cam_Disk_4X.MCF" PostProcessor="F50_Fanuc" NcFile="SBO.NC"/>
            <Stock Number="1">
                <Material Name="Zirconia.mtr" Class="Zirconia">
                    <Dimension Diameter="94" Width="93.9804412498" Thickness="26"/>
                    <Shoulder Define="No"/>
                    <Teeth Number="1">
                        <Tooth Typology="anatomicCrown">
                            <STL DirectoryName="U:CAD InterfacesSTLCoping - CrownCrown Single" FileName="0016  ADVANCED KELLY B1_0.STL" FacetNumber="42782"/>
                            <Opelist DirectoryName="Milling CrownTools - Ball-D2.0-D1.0-D0.6" FileName="2 - Zirconia - Crown - D2-D1-D0.6 - Standard.OPL"/>
                        </Tooth>
                    </Teeth>
                </Material>
            </Stock>
            <Machining Number="1">
                <Cycle Name="Roughing D2 at 0" Type="411" ToolName="Zr Ball End Mill D2.F14" ToolPosition="1"/>
            </Machining>
            <ToolList Number="1">
                <Tool Name="Zr Ball End Mill D2.F14" Type="460" Diameter="2" Radius="1"/>
            </ToolList>
        </Project>
    </GO2cam_to_PPU>';





    /*
        An array of Objects - each Object has the XPath expression that 
        will identify a particular element in the XML and the attribute
        for which a value will be found.
    */
    $queries=array(
        (object)array('expr'=>'//Software/OEM','attr'=>'Name'),
        (object)array('expr'=>'//Project/Stock/Material','attr'=>'Name'),
        (object)array('expr'=>'//Project/Stock/Material/Dimension','attr'=>'Thickness'),
        (object)array('expr'=>'//Project/Stock/Material/Teeth/Tooth/STL','attr'=>'FileName'),
        (object)array('expr'=>'//Project/Stock/Material/Teeth/Tooth/Opelist','attr'=>'FileName')
    );
    
    /*
        Create the basic DOMDocument object
        and set some runtime options for libxml
    */
    libxml_use_internal_errors( true );
    $dom=new DOMDocument;
    $dom->validateOnParse=false;
    $dom->recover=true;
    $dom->strictErrorChecking=false;
    /*
        Load the string of XML data
        or, use `$dom->load($filepath)` 
        to load a file.
    */
    $dom->loadXML($strxml);
    
    /*
        grab reference of any errors
    */
    $errors=libxml_get_errors();
    libxml_clear_errors();
                    
    
    /*
        Create the basic XPath Object that
        will be used to query the XML data.
    */
    $xp=new DOMXPath($dom);
    
    /*
        Iterate through the queries array
    */
    foreach( $queries as $obj ){
        # construct the XPath query & run it.
        $col=$xp->query( $obj->expr );
        # process the results
        if( $col->length > 0 ){
            # find first item in results and find the appropriate attribute
            $node=$col->item(0);
            printf('%s: %s<br />', $node->tagName, $node->getAttribute( $obj->attr ) );
        }
    }
    
    #show any errors...
    if( !empty( $errors ) )printf('<pre>%s</pre>',print_r($errors,true));
?>

This will output:

OEM: DEMO Disk 4X
Material: Zirconia.mtr
Dimension: 26
STL: 0016 ADVANCED KELLY B1_0.STL
Opelist: 2 - Zirconia - Crown - D2-D1-D0.6 - Standard.OPL
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement