Jump to content

Parsing XML from a Query with PHP


kts

Recommended Posts

So I have a dataset that is coming back in the version of XML (although not sure if its older, or just not the standard format) it looks like this:

<result>
<rows>
<row>
<column name="osis number">209959568</column>
<column name="Last Name">Lee</column>
</row>
<row>
<column name="osis number">264978495</column>
<column name="Last Name">Stover</column>
</row>
<row>

<column name="osis number">267274983</column>
<column name="Last Name">Cruz</column>
</row>
<row>
<column name="osis number">215475278</column>
<column name="Last Name">Henderson</column>
</row>
<row>
<column name="osis number">265384750</column>
<column name="Last Name">Christian</column>
</row>

<row>
<column name="osis number">267445849</column>
<column name="Last Name">Montes</column>
</row>
<row>
<column name="osis number">267545127</column>
<column name="Last Name">Jordan</column>
</row>
<row>
<column name="osis number">214962268</column>
<column name="Last Name">Lane</column>

</row>
<row>
<column name="osis number">265608117</column>
<column name="Last Name">Rice</column>
</row>
<row>
<column name="osis number">204897672</column>
<column name="Last Name">French</column>
</row>
</rows>
</result>

 

I am trying to use existing functions like xml2array, although you can see my XML isn't in standard format of <osis number>204897..etc</osis number>

 

Has anyone had to parse XML that looks like this? Also, is it coming out this way because of the spaces in the names? or is just an older style?

 

Thanks

Link to comment
Share on other sites

It seems like you are trying to parse xml attributes:

 

<column [b]name="osis number"[/b]>209959568</column>

 

I googled "parsing xml attributes with php" and hit these links:

http://paulstamatiou.com/how-to-parse-xml-with-php5

http://www.webmasterworld.com/forum26/326.htm

 

I hope one of those will get you started in the right direction  8)

Keep us posted.

Link to comment
Share on other sites

Hey Jiin thanks the first link seems to be the way I need to go! But I am receiving an error with:

 

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found in

 

I have seen it in many ways of attempting this... I believe its due to the format of the XML... Not sure

Link to comment
Share on other sites

You will have to put it in a foreach loop but here is a good start:

<?php
$xmlfile = simplexml_load_file('t.xml');
$colname =  $xmlfile->xpath('/result/rows/row/column/@name');
echo $colname[0];
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.