/    Sign up×
Community /Pin to ProfileBookmark

Reading XML with PHP Issue

The code below is just a made up example of what i am looking to do.

Firstly i cant change the layout of the XML file due to it is pulled from a external source then i want to display it and save to a mysql DB

I can display and save the information that is in open and closed attributes like this one

<description>Cars made from Honda</description>

I was wondering if it is possible if i could pull the information below that is in bold and save it to a db

<row [B]typeName=”Honda” groupID=”266″ typeID=”11584″[/B]>

[CODE]
<row groupName=”Cars” groupID=”266″>
<rowset name=”manufactor” key=”typeID” columns=”typeName,groupID,typeID”>
<row typeName=”Honda” groupID=”266″ typeID=”11584″>
<description>Cars made from Honda</description>
<rank>3</rank>
<rowset name=”year” key=”typeID” columns=”typeID”/>
<requiredAttributes>
<primaryAttribute>wheels</primaryAttribute>
<secondaryAttribute>engine</secondaryAttribute>
</requiredAttributes>
</row>
[/CODE]

Any help on this matter would be great or if you need anymore info just say

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarAug 07.2008 — quick question first.

Have you got some existing code to read the file in(as a starting point, since there are several ways to access and XML file I don't want to assume and be wrong)?

http://php.net/manual/en/function.simplexml-element-attributes.php
Copy linkTweet thisAlerts:
@Chris_CrewauthorAug 08.2008 — i have been using the following code to read the xml which works for everything but attributes.

I will have a look over the link you gave cause that seems like what i need just need to work out how to get it working for my needs

[code=php]<?php

$objDOM = new DOMDocument();
$objDOM->load("skilltree.xml");


$row = $objDOM->getElementsByTagName("rowset");

foreach( $row as $value )
{
$charIDs1 = $value->getElementsByTagName("rowset");
$charID1 = $charIDs1->item(0)->nodeValue;

echo "$charID1<br/>";
}


?> [/code]
Copy linkTweet thisAlerts:
@scragarAug 08.2008 — [code=php]<?php

$objDOM = new DOMDocument();
$objDOM->load("skilltree.xml");


$row = $objDOM->getElementsByTagName("rowset");

foreach( $row as $value )
{
echo $value->getattribute('typeName') . "n<br>n";// looky here

$charIDs1 = $value->getElementsByTagName("rowset");
$charID1 = $charIDs1->item(0)->nodeValue;

echo "$charID1<br/>";
}


?> [/code]

http://www.php.net/manual/en/domelement.getattribute.php

http://www.php.net/manual/en/class.domelement.php
Copy linkTweet thisAlerts:
@Kostas_ZotosAug 08.2008 — Hi,

Another example also here (use DOM and an alternative XPath approach):

(BTW: Your xml need some fix to be well-formed)

Assume this is a xml file, named: [B]data.xml[/B]
[code=php]<?xml version="1.0" encoding="utf-8"?>
<data>
<item groupName="Cars" groupID="266">
<rowset name="manufactor" key="typeID" columns="typeName,groupID,typeID"/>
<row typeName="Honda" groupID="266" typeID="11584"/>
<description>Cars made from Honda</description>
<rank>3</rank>
<rowset name="year" key="typeID" columns="typeID"/>
<requiredAttributes>
<primaryAttribute>wheels</primaryAttribute>
<secondaryAttribute>engine</secondaryAttribute>
</requiredAttributes>
</item>
</data>[/code]


Here is a php suggestion:

(shows 2 ways to access the attributes: one based on the DOM functions and an alternative based on Xpath expressions):
[code=php]<?php
// -------------------------------------- CREATE A NEW DOM XML OBJECT ----------------------------------------

$doc = new DOMDocument; // Create a new dom document
$doc->preserveWhiteSpace = false; // Eliminate whitespaces (empty lines, etc)
// $doc->formatOutput = true; // Create indents on xml

$doc->load( 'data.xml' ); // LOAD THE FILE


// ------------------------------------------- GET ALL row ELEMENTS -----------------------------------------------

$rows=$doc->getElementsByTagName("row"); // Returns a nodeList with all the "row" elements in our xml file
// Use: $rows->item(0) to acces the 1st "row" element, $rows->item(1) (is the 2nd element), etc..
// In our xml example there is only one "row" element. ( The total "row" elements are: $rows->length; )


// ----------------------------------------- GET THE ATTRIBUTES VALUES --------------------------------------

// Will get the attribute values of the 1st "row" (<row typeName="Honda" groupID="266" typeID="11584"/>)


// ------------- METHOD 1 ) ( USING THE DOM ) ------------

$typeName=$rows->item(0)->getAttribute("typeName"); // Get the "typeName"

// $rows->item(0) is the 1st "row" element -in our example there is only one-
// OR:

//$typeName=$rows->item(0)->attributes->item(0)->nodeValue;


$groupID=$rows->item(0)->getAttribute("groupID"); // Get the "groupID"

$typeID=$rows->item(0)->getAttribute("typeID"); // Get the "typeID"


// ---------- METHOD 2 ) ( USING XPATH ) ---------------

/* // This rquires a basic knowledge of Xpath expressions
$xpath = new DOMXPath($doc);

$typeName=$xpath->query('/data/item/row[1]/@typeName')->item(0)->nodeValue; // Get the "typeName"
$groupID=$xpath->query('/data/item/row[1]/@groupID')->item(0)->nodeValue; // Get the "groupID"
$typeID=$xpath->query('/data/item/row[1]/@typeID')->item(0)->nodeValue; // Get the "typeID"

*/


// ------------------------------------------------ DISPLAY THE ATTRIBUTES --------------------------------------------
echo "
<b> typeName: </b> $typeName <br>
<b> groupID: </b> $groupID <br>
<b> typeID: </b> $typeID <br>
";

?> [/code]


Regards!

Kostas
×

Success!

Help @Chris_Crew spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...