-2

Possible Duplicate:
Implementing condition in XPath and XQuery
PHP convert XML to JSON

How do i get all child notes from the record which includes Product = Product1 And put them into a JSON array ?

<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt1]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[10]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt2]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[20]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt3]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[30]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>

I need the data for an iPad App, calling something like www.products.com/product.php?Product1

Having the JSON look something like

{
 "Product": [
 { "Name":"Product1" , "Value":"10" , "Status":"Active" }
 ]
 }

Edit : Solution with XML reader

<?php
$z = new XMLReader;
$z->open('products.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($z->read() && $z->name !== 'RECORD');

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'RECORD')
{

    $node = simplexml_import_dom($doc->importNode($z->expand(), true));


    $strvalue = $node->PROP[6]->PVAL;
    echo $strvalue."<p>" ;

    // go to next <product />
    $z->next('RECORD');
}
?>
3
  • There's nothing like json array. Do you mean json formatted string? Commented Oct 4, 2012 at 13:28
  • To my knodledge there is a thing called JSON Array, but true - it's made of JSON Strings/objects w3schools.com/json/json_syntax.asp Commented Oct 4, 2012 at 13:48
  • @UlrikVadstrup I was talking about php. This article may help you. Commented Oct 4, 2012 at 13:53

1 Answer 1

1

First off, just looking at your XML gives me a headache. Here are a few changes you need to make:

  1. If you are going to have multiple nodes, then you need a wrapper such as before all of the nodes and after all of them.
  2. Your PROP node needs a closing tag
  3. Your CDATA is misformatted, the proper format is

Here's my example of the newly formatted XML:

<?xml version='1.0'?>
<RECORDS>
    <RECORD>
    <PROP NAME="Product">
        <PVAL><![CDATA[Produkt1]]></PVAL>
    </PROP>
    <PROP NAME="Value">
        <PVAL><![CDATA[10]]></PVAL>
    </PROP>
    <PROP NAME="Status">
        <PVAL><![CDATA[Active]]></PVAL>
    </PROP>
</RECORD>
<RECORD>
    <PROP NAME="Product">
        <PVAL><![CDATA[Produkt2]]></PVAL>
    </PROP>
    <PROP NAME="Value">
        <PVAL><![CDATA[20]]></PVAL>
    </PROP>
    <PROP NAME="Status">
        <PVAL><![CDATA[Active]]></PVAL>
    </PROP>
</RECORD>
<RECORD>
    <PROP NAME="Product">
        <PVAL><![CDATA[Produkt3]]></PVAL>
    </PROP>
    <PROP NAME="Value">
        <PVAL><![CDATA[30]]></PVAL>
    </PROP>
    <PROP NAME="Status">
        <PVAL><![CDATA[Active]]></PVAL>
    </PROP>
</RECORD>

Once the XML is formatted properly, I'd use something like SimpleXML to parse it:

$xml = simplexml_load_string( $xml_string );
foreach ($xml->RECORD as $record)
{
    if ($record->PROP[0]->PVAL == 'Produkt1')
    {
        echo json_encode( $record );
    }
}

I also think you could really benefit from restructuring the entire XML. Something like:

<RECORDS>
    <RECORD ID="1">
        <PRODUCT>PRODUCT 1</PRODUCT>
        <VALUE>10</VALUE>
        <STATUS>Active</STATUS>
    </RECORD>
    <RECORD ID="2">
        <PRODUCT>PRODUCT 2</PRODUCT>
        <VALUE>20</VALUE>
        <STATUS>Active</STATUS>
    </RECORD>
    <RECORD ID="3">
        <PRODUCT>PRODUCT 3</PRODUCT>
        <VALUE>30</VALUE>
        <STATUS>Active</STATUS>
    </RECORD>
</RECORDS>

So then the PHP would be like:

foreach ($xml->RECORD as $record)
{
    $attr = $record->attributes();
    if( $attr['ID'] == '1' )
    {
        echo json_encode( $record );
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for a constuctive answer :-) Unfortunatly i'm not the one making the xml file, i have just been told to drag out info. I will make my own example and use your code to get him to change things. I will test later - Thanks a million :-D
hmn.....I'm for sure on the right track. When i created a "small" test file, everything works as planned. However my XML file is 100 mb, so i guess i should use XMLreader instead of SimpleXML. Anybody can give me a little help ? writing Jason's code for xmlreader ?
How do i read out the "Name" Value - like "Status" ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.