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 7943275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:13:28+00:00 2026-06-04T00:13:28+00:00

Hello All! I’m active in a fairly large project, but I have limited experience

  • 0

Hello All!

I’m active in a fairly large project, but I have limited experience with XML. I am dynamically generating XML, data which may be customed to needs of individual customers. The current solution has been (please don’t hurt me, I’m the new guy) to inline a php template by include(). This is not good practice and I want to move to a better solution.

Structure

<?xml version='1.0'?>
<Product id="">
  <AswItem></AswItem>
  <EanCode></EanCode>
  <ImagePopup></ImagePopup>
  <ImageInfo></ImageInfo>
  <ImageThumbnail></ImageThumbnail>
  <PriceCurrency></PriceCurrency>
  <PriceValueNoTax></PriceValueNoTax>
  <Manufacture></Manufacture>

  <ProductDescriptions>
    <ProductDescrition language="" id="">
      <Name></Name>
      <Description></Description>
      <Color></Color>
      <Size></Size>
      <NavigationLevel1></NavigationLevel1>
      <NavigationLevel2></NavigationLevel2>
      <NavigationLevel3></NavigationLevel3>
      <NavigationLevel4></NavigationLevel4>
    </ProductDescrition>

  </ProductDescriptions>

  <MatrixProducts>
    <AswItem></AswItem>
    <EanCode></EanCode>
    <ParentId></ParentId>
    <PriceCurrency></PriceCurrency>
    <PriceValueNoTax></PriceValueNoTax>
    <ImagePopup></ImagePopup>
    <ImageInfo></ImageInfo>
    <ImageThumbnail></ImageThumbnail>
  </MatrixProducts>
</Product>

This is our main structure. ProductDescriptions and MatrixProducts are basically list items, and may contain none to several children. Our object to be translated into XML is a PHP hash tree with a similar structure but with different keys.

Problem

The problem I have is that I get stuck in the thought process on how dynamically create a tree from an object. My current plan is to have a key conversion table (see Current Solution) but a voice in the back of my head is telling me that it’s not best practice.

Previous solution

populate.php

foreach($products as $product) {
   
    // too much black magic in here
    include($chosenTemplate);

    // $productXMLString is generated in the previous include
    printToXML($productXMLString)
   
}

template.php

<? 
echo "<Product id='{$product['id']}'>";
// etc...
echo "</product>";

As you can see, this is a pretty bad approach. Bad error handling, messy syntax and lot’s of other quirks.

Current solution

    $xmlProductTemplate = simplexml_load_file($currentTemplate);

    foreach($products as $product) {
    $xmlObj = clone $xmlProductTemplate;
        foreach($product as $key => $productValue) { 
    // if value is a <$key>$string</$key>, just input 
    // it into the translated key for the $xmlObject
    if(!is_array($productValue))
        $xmlObj[translateKeyToXML($key)] = $productValue;

    // elseway, we need to call the magic; traverse a child array
    // and still hold true to templateing
    else {
        // what DO you do?
    }
    }
// save xml
fputs($xmlObj->asXML());
    }

How would you go about this and what is best practice? I’m a bit hungry and dehydrated so please tell me if I’m missing something basic.

  • 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-04T00:13:30+00:00Added an answer on June 4, 2026 at 12:13 am

    I am having a bit of trouble understanding what you’re trying to do so excuse me if I’m off here. What it seems like you are trying to do is create an XML file based on a “template” with an ArrayObject containing the attributes and values of the XML elements.

    Perhaps, instead of trying to do that, you just create a SimpleXML object. I think that would be much easier for what you’re trying to do and it adds the value of error catching. See SimpleXML on PHP.net.

    If I am not on the right track with an answer, can you post more source code like the class that contains the values? Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello all I have a small dialog which I created dynamically, which has a
Hello all I'm using NSXMLParser to parse some xml data. I'm using this data
hello all i have a json file i.e { data: [ { name: The
Hello all i have an app it works fine on API 8 but i
Hello All, I have the following molten data: X variable value 1 StationA SAR11.cluster
hello all i am working on a project in which i have a webpage
Hello all i'm turning objects on (adding the class .active) and off on a
hello all this seems to be my problem I have a table in mysql
Problem Hello all! I have this code which takes my jpg image loops through
hello guys I am trying to extract all the anchor links from aol but

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.