Page 1 of 1

XML From Scratch : SOLVED

Posted: Thu Mar 24, 2016 9:43 am
by victor Lukenga
Hello !

I tried, without success to create an XML from scratch :
the Idea is to take an existing dataset, revome unwanted nodes and write new ones based on previous events.

here is the production XML:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<order>
<productnumber>29733</productnumber>
<ordernumber>ORD230316318715T_3591180001</ordernumber>
<productname>product </productname>
<qte>1</qte>
<nbitems>1</nbitems>
<orderitemnumber>1</orderitemnumber>
<item>359118</item>
</order>
preflight xml :

Code: Select all

1<EnfocusReport>
2  <Report>
3    <PreflightResult errors="0" criticalfailures="0" noncriticalfailures="0" signoffs="0" fixes="0" warnings="1">
4     <PreflightResultEntry type="Check" level="warning">
5      <PreflightResultEntryMessage xml:lang="fr-FR">
       <Message>La résolution de image couleur ou en gamme de gris est inférieur(e) à 150 ppi (1x à la page 1)</Message>
          <StringContext>
            <BaseString>La résolution de %ImageType% est %Comparator% %ReferenceResolution% ppi</BaseString>
            <Const name="ActionID">1150</Const>
            <Const name="ImageType">image couleur ou en gamme de gris</Const>
            <Const name="Category">Check</Const>
            <Const name="Comparator">inférieur(e) à </Const>
            <Const name="ReferenceResolution">150</Const>
            <Instance>
              <Location maxX="454.8" maxY="347.4" minX="-1.6e-008" minY="-1.6e-008" page="0"/>
I want to Extract

Code: Select all

<ordernumber>ORD230316318715T_3591180001</ordernumber>
<item>359118</item>
and the

Code: Select all

Warning 
or

Code: Select all

error
tag in line 4



The new xml will look like :

Code: Select all

<ProcessedFile>
		<ordernumber>ORD230316318715T_3591180001</ordernumber>
		<item>359118</item>
			<Result state ="warning"(or error)> 
				<path>192.168.1.69/paoflow/ORD230316318715/359118</path>
			</result>
</processedFile>
if there is no preflight issue

Code: Select all

<Result state ="ok">
Hope you can help me because IT wait for me, and impossible to construct my xml :(

Thank you

Victor

Re: XML From Scratch

Posted: Thu Mar 24, 2016 5:05 pm
by cstevens
Here's the basis for creating your XML file, I'll leave it to you to map the data from the preflight XML

Code: Select all

	//Create a new blank XML document	
	var newXML = new Document();
	
	//Create a root element named "ProcessedFile"	
	var rootElem = newXML.createElement("ProcessedFile");
	newXML.setDocumentElement(rootElem);
	
	//Create the ordernumber element and text	
	var orderNum = newXML.createElement("ordernumber");
	rootElem.appendChild(orderNum);
	var orderTxt = newXML.createText("your order number");
	orderNum.appendChild(orderTxt);
	
	//Create the item element	
	var item = newXML.createElement("item");
	rootElem.appendChild(item);
	
	//Create the Result element	
	var result = newXML.createElement("Result");
	item.appendChild(result);
	
	//Add a state attribute to the Result element	
	result.addAttribute("state", null, "warning");	
	
	//Create a temporary location to save the XML, save then send to next	
	var xmlPath = job.createPathWithName("processedFile.xml");
	newXML.save(xmlPath);
	job.sendToSingle(xmlPath);

Re: XML From Scratch

Posted: Fri Mar 25, 2016 9:24 am
by jan_suhr
It's easier to export the dataset to XML with the Export Metadata element and then use Saxon and XSLT to construct the XML-file you want.


Just my $0.02


Jan

Re: XML From Scratch // SOLVED

Posted: Tue Apr 05, 2016 3:32 pm
by victor Lukenga
Hello

First thanks for your help

I manage write a script wich work pretty well based on your help and a script I saw to creat a txt file

Code: Select all


var dataset = job.getDataset("dataset");
var mymetatadata = dataset.evalToString(/xpath.../);
var my2ndmetatadata = dataset.evalToString(/xpath.../);

var someOtherMetaData = dataset.evalToString(...);
var someOtherMetaData = dataset.evalToString(...);

//Create and Set Xml file name
	//var Name = job.getNameProper();
	var path = job.createPathWithName(orderId + '_' + itemId + ".xml");job.log(-1, path);
	var f = new File(path);
	
	
//write xml datas in file
f.open(File.WriteOnly);
f.writeLine('<?xml version="1.0" encoding="ISO-8859-1"?>');

//nodes//

f.writeLine("<order>"); // Parent
f.writeLine("<ordernumber>"+ orderId + '_' + (mymetadata)+"</ordernumber>"); //child
f.writeLine("<state>"+ (my2ndmetadata)+"</state>");
f.writeLine("</order>");//end of Parent


// Close and Send xml file to next folder
f.close();
job.sendToSingle( path );
}
Thank's a gain this forum is a goldmine !