Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:41:38+00:00 2026-06-12T11:41:38+00:00

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

  • 0

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 http://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');
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:41:40+00:00Added an answer on June 12, 2026 at 11:41 am

    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 );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the fastest XML parser in PHP? I still need to
Possible Duplicate: Count elements for objects implementing ArrayAccess using count()? In PHP 5, you
Possible Duplicate: MySql: MyISAM vs. Inno DB! I am implementing a blog in php
Possible Duplicate: Accessing inherited variable from templated parent class I have been implementing a
Possible Duplicate: php warning mysql_fetch_assoc i am just implementing a simple part of my
Possible Duplicate: StructureMap singleton usage (A class implementing two interface) I'm currently designing a
Possible Duplicate: Facebook's horizontal scrollbar in ticker & chat sidebar I am implementing a
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.