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 Interfaces\STL\Coping - Crown\Crown Single" **FileName="0016 ADVANCED KELLY B1_0.STL"** FacetNumber="42782"/>
<Opelist DirectoryName="Milling Crown\Tools - 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>
To accomplish the goal of displaying various Attributes from some XML data using PHP the most common method would be to useDOMDocument
andDOMXPath
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 Interfaces\STL\Coping - Crown\Crown Single" FileName="0016 ADVANCED KELLY B1_0.STL" FacetNumber="42782"/>
<Opelist DirectoryName="Milling Crown\Tools - 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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.