467,077 Members | 996 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

E4X XML creation and xmlns in nested elements: help request

I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as:

Expand|Select|Wrap|Line Numbers
  1. <store
  2.     xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  3. <product sku="10050-1653" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  4.     <sku>10050-1653</sku>
  5.     <name xml:lang="x-default">shop's Foie Gras</name>
  6.     <online>1</online>
  7.     <manufacturer>
  8.         <manufacturer-name>shop's</manufacturer-name>
  9.     </manufacturer>
  10.     <product-list-prices>
  11.         <product-list-price currency="USD">95</product-list-price>
  12.     </product-list-prices>
  13.     <variations>
  14.      <mastered-products>
  15.             <mastered-product default="1" sku="10050-1652"/>
  16.             <mastered-product sku="10050-1653"/>
  17.         </mastered-products>
  18.         <variation-attributes>
  19.             <variation-attribute name="Size">
  20.                 <custom-attributes>
  21.                     <custom-attribute name="displayName" type="string" xml:lang="x-default">Size</custom-attribute>
  22.                 </custom-attributes>
  23.             </variation-attribute>
  24.         </variation-attributes>
  25.     </variations>
  26.     <custom-attributes>
  27.         <custom-attribute name="Item_Ship_Weight" type="double" xml:lang="x-default">1.25</custom-attribute>
  28.         <custom-attribute name="Size" type="string" xml:lang="x-default"> 14 oz. no tin</custom-attribute>
  29.     </custom-attributes>
  30. </product>
  31. </store>
That is what the output XML should look like

This is what my ouput looks like

Expand|Select|Wrap|Line Numbers
  1. <store xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  2.     <product sku="1110001" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  3.         <sku>1110001</sku>
  4.     <ProductName>shop's Special Blend Coffee</ProductName>
  5. <long-description>Our TESTING house blend and our customers favorite. Full-bodied, flavorful - a perfect all around blend.</long-description>
  6.    <online>1</online>
  7. <manufacturer>
  8. <manufacturer-name>shops</manufacturer-name>
  9. </manufacturer>
  10. <product-list-prices>
  11. <Amount>6.98</Amount>
  12. <product-list-price currency="USD"/>
  13. </product-list-prices>
  14. <custom-attributes xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  15. <custom-attribute name="PID_VALUE_ManufacturerID@global" type="string" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">60100</custom-attribute>
  16. <custom-attribute name="LegacyItemNumber" type="string" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">60100</custom-attribute>
  17. <custom-attribute name="InStoreOnly" type="string" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">N</custom-attribute>
  18. <custom-attribute type="string" name="Grind" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog"/>
  19. <custom-attribute name="HTMLDescription" type="html" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">Our TESTING house blend and our customers favorite. Full-bodied, flavorful - a perfect all around blend.</custom-attribute>
  20. </custom-attributes>
  21. <variations xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  22. <variation-attributes xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  23. <variation-attribute xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  24. <custom-attributes>
  25. <custom-attribute name="displayName" string="string">Grind</custom-attribute>
  26. </custom-attributes>
  27. </variation-attribute>
  28.  </variation-attributes>
  29. <mastered-products xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog">
  30.  <mastered-product sku="1110001-001" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog"/>
  31.        <mastered-product sku="1110001-002" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog"/>
  32.       <mastered-product sku="1110001-003" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog"/>
  33.         <mastered-product sku="1110001-004" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog"/>
  34.         <mastered-product sku="1110001-005" xmlns="http://www.store.com/xml/1.1.0.0/impex/catalog"/>
  35.       </mastered-products>
  36.     </variations>
  37. </product>
  38. </store>
Now forget that some of these values don't 100% map up, the real problem I am having is with the xmlns. I do not want it set at the levels it is being set at, I only want it set for the top 2 levels. The main store element and the product elements under it.

What I can tell is that in my code I am creating new xml elements to deal with the custom-attributes because it is a list and I do not know any other way to do it. It seems when ever I create new XML instead of build off of the main XML I get the NS set.

So my questions are:

1. How do I build multiple levels deep of lists in another way than I have done in my code? (see the :

var customAttributesXML = <custom-attributes></custom-attributes>;
and the variations section below it, which is nest 3 times.
section of the code)

2. Is there a way to "unset" the xmlns for particular elements of the XML.

This is all in the Rhino JS engine.

My code is below:

Expand|Select|Wrap|Line Numbers
  1. var finalDWCatalogXML;
  2.     var productArrayList = new java.util.ArrayList();
  3.  
  4.     // pull the itemData out of the WIRXML for looping
  5.     var defaultNS = new Namespace('http://www.store.com/xml/1.1.0.0/impex/catalog');
  6.     default xml namespace = defaultNS;
  7.  
  8.     // start the DW catalog XML
  9.     finalDWCatalogXML = new XML('<store></store>');
  10.  
  11.     // Traverse the WIRXML item data
  12.     for(i in WIRItemData)
  13.     {
  14.         item = new XML(WIRItemData[i]);
  15.         productXMLHash = getProductAttributes(item);
  16. finalDWCatalogXML.appendChild(productXMLHash.get("masterProductXML"));
  17.  
  18.     } // close i WIRItemData loop
  19.  
  20.     print("finalDWCatalogXML = " + finalDWCatalogXML.toString());
  21.  
  22.     var exportString = finalDWCatalogXML.toString();
  23.  
  24.     return PIPELET_NEXT;
  25.  
  26.  
  27.  
  28. // ---------------------------------------------------------------------------------------
  29. // ---------------------------------------------------------------------------------------
  30.  
  31. function getProductAttributes(inXML)
  32. {
  33.     var finalOutputHash;
  34.     var productOutputXML = <product></product>;
  35.  
  36.     // sku
  37.     if(inXML.@itemCode != "")
  38.     {
  39.         productOutputXML.@sku = inXML.@itemCode;
  40.         productOutputXML.sku = inXML.@itemCode;
  41.     }
  42.  
  43.     // name
  44.     if(inXML.ProductName != "")
  45.     {
  46.         productOutputXML['name'] = inXML.ProductName;
  47.     }
  48.  
  49.     // long-description
  50.     if(inXML.InfoText.toString() != "")
  51.     {
  52.         productOutputXML['long-description'] = inXML.InfoText.toString();
  53.     }
  54.  
  55.     if(inXML.@isTaxable != "")
  56.     {
  57.         if(inXML.@isTaxable == "True")
  58.             productOutputXML['tax-class'].@id = "FullTax";
  59.         else
  60.             productOutputXML['tax-class'].@id = "NoTax";
  61.     }
  62.  
  63.     if(inXML.AttributeData.Attribute.(@name == "Brand").toString() != "")
  64.     {
  65.         productOutputXML.brand = inXML.AttributeData.Attribute.(@name == "Brand").toString();
  66.         productOutputXML.manufacturer['manufacturer-name'] = inXML.AttributeData.Attribute.(@name == "Brand").toString();
  67.     }
  68.  
  69.  
  70.     if(inXML.PriceData.Price.Amount != "")
  71.     {
  72.         productOutputXML['product-list-prices']['product-list-price'] = inXML.PriceData.Price.Amount;
  73.         productOutputXML['product-list-prices']['product-list-price'].@currency = "USD";
  74.     }
  75.  
  76.     // custom attributes
  77.     var customAttributesXML = <custom-attributes></custom-attributes>;
  78.  
  79.     if(inXML.AttributeData.Attribute.(@name == "Legacy Product ID").toString() != "")
  80.     {
  81.         customAttributesXML['custom-attribute'] += <custom-attribute name="PID_VALUE_ManufacturerID@global" type="string">{inXML.AttributeData.Attribute.(@name == "Legacy Product ID").toString()}</custom-attribute>;
  82.     }
  83. ...
  84.  
  85.     if(inXML.AttributeData.Attribute.(@name == "Regular Price").toString() != "")
  86.     {
  87.         customAttributesXML['custom-attribute'] += <custom-attribute name="RegularPrice" type="double">{inXML.AttributeData.Attribute.(@name == "Regular Price").toString()}</custom-attribute>;
  88.     }
  89.  
  90.  
  91.     var variationsXML = <variations></variations>;
  92.     var variationAttributesXML = <variation-attributes></variation-attributes>;
  93.     var variationCustomAttributesXML = <custom-attributes></custom-attributes>;
  94.  
  95.     for(j in inXML.DimensionData.Dimension)
  96.     {
  97.         dimensionXML = inXML.DimensionData.Dimension[j];
  98.  
  99.         variationAttributesXML['variation-attribute'] += <variation-attribute><custom-attributes><custom-attribute name = "displayName" string = "string">{dimensionXML.@name.toString()}</custom-attribute></custom-attributes></variation-attribute>
  100.     }
  101.  
  102.     variationsXML.appendChild(variationAttributesXML);
  103.  
  104.     var masteredProductsXML = <mastered-products></mastered-products>;
  105.     var masteredProductXML;
  106.  
  107.     for(x in inXML.SubItem)
  108.     {
  109.         subItemXML = inXML.SubItem[x];
  110.         masteredProductsXML['mastered-product'] += <mastered-product sku={subItemXML.@itemCode}></mastered-product>;
  111.  
  112.  
  113.     }
  114.  
  115.  
  116.     variationsXML.appendChild(masteredProductsXML);
  117.     productOutputXML.appendChild(variationsXML);
  118.     productOutputXML.appendChild(customAttributesXML);
  119.     finalOutputHash    = new java.util.HashMap();
  120.     finalOutputHash.put("masterProductXML" , productOutputXML);
  121.  
  122.     return finalOutputHash;
  123. }
Aug 31 '06 #1
  • viewed: 2522
Share:

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Bob Smith | last post: by
1 post views Thread by jorgen.wahlund@gmail.com | last post: by
4 posts views Thread by Shailendra Batham | last post: by
6 posts views Thread by fzhang@calamos.com | last post: by
reply views Thread by Ralfeus | last post: by
2 posts views Thread by eliasen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.