Copy link to clipboard
Copied
I have some xml file
<?xml version="1.0" encoding="utf-8"?>
<Comment Name="JobDescription">Test job</Comment>
<Comment Name="Description">Customer request</Comment>
<Comment Name="Papergrade">300g</Comment>
<Comment Name="Printmethod">Offset</Comment>
<Comment Name="Iteration">001</Comment>and I need to read some values from this xml in PS,
I read and alert whole xml file with this js,
var XMLfile = new File("/Users/rsmilevicm/Documents/test.xml");
XMLfile.open('r');
var str = "";
while(!XMLfile.eof)
str += XMLfile.readln();
XMLfile.close();
alert(str);But is it possible to alert just values such as: Test job, Customer request,...
Thanks in advance.
Try the following
var XMLfile = new File("/Users/rsmilevicm/Documents/test.xml");
XMLfile.open('r');
var str = "";
str = XMLfile.read();
XMLfile.close();
var xml = new XML(str)
for(var i = 0; i < xml.children(0).length(); i++)
alert (xml.children(0)[i])
Also, your XML seems to be invalid you should have a root node that encloses every other node. Something like the following should work
<?xml version="1.0" encoding="utf-8"?>
<doc>
<Comment Name="JobDescription">Test job</Comment>
<Comment ...
Copy link to clipboard
Copied
Try the following
var XMLfile = new File("/Users/rsmilevicm/Documents/test.xml");
XMLfile.open('r');
var str = "";
str = XMLfile.read();
XMLfile.close();
var xml = new XML(str)
for(var i = 0; i < xml.children(0).length(); i++)
alert (xml.children(0)[i])
Also, your XML seems to be invalid you should have a root node that encloses every other node. Something like the following should work
<?xml version="1.0" encoding="utf-8"?>
<doc>
<Comment Name="JobDescription">Test job</Comment>
<Comment Name="Description">Customer request</Comment>
<Comment Name="Papergrade">300g</Comment>
<Comment Name="Printmethod">Offset</Comment>
<Comment Name="Iteration">001</Comment>
</doc>
-Manan
Copy link to clipboard
Copied
@Manan Joshi Thank you for quick and correct respons, I marked this as correct answer, but could you help me how to choose just one value, for example if i need value from prinmethod?
P.S. Keep in mind, this is just an example, my original xml have hundreds rows, and different tags.
Copy link to clipboard
Copied
Everything how to do it is described in 'JAVASCRIPT TOOLS GUIDE'.
Copy link to clipboard
Copied
You can also use the following sample syntax to read specific values:
var myFile = File(pathToFile);
myFile.encoding = 'utf-8';
myFile.open('r');
var myXML = new XML(myFile.read());
myFile.close();
var x = myXML.data.(@key == 'myKey').@value;
Copy link to clipboard
Copied
For that, you need to use XPaths. They are used to navigate the elements of an XML. In the following code, I use an Xpath to locate all the Comment nodes under doc that have the value of Name attribute set to Printmethod
var XMLfile = new File("/Users/manan/Desktop/test/test.xml");
XMLfile.open('r');
var str = "";
str = XMLfile.read();
XMLfile.close();
var xml = new XML(str)
var r = xml.xpath("/doc/Comment[@Name='Printmethod']")
for(var i = 0; i < r.length(); i++)
alert (r[i])
You can start reading about XPaths from XPath Tutorial
-Manan
Copy link to clipboard
Copied
Thanks a lot @Manan Joshi @Kukurykus
Copy link to clipboard
Copied
Dear @Manan Joshi do you have any idea what might be the problem when working with jdf document?
When I have file like this everthing works perfectly with your code
<?xml version="1.0" encoding="utf-8"?>
<JDF>
<Comment Name="JobDescription">Test job</Comment>
<Comment Name="Description">Customer request</Comment>
<Comment Name="Papergrade">300g</Comment>
<Comment Name="Printmethod">Offset</Comment>
<Comment Name="Iteration">001</Comment>
</JDF>but when i have some xml like this below something went wrong
<?xml version="1.0" encoding="utf-8"?>
<JDF xmlns="http://www.CIP4.org/JDFSchema_1_1" DescriptiveName="" ID="" JobID="" JobPartID="" ProjectID="" RelatedJobID="" Status="Setup" Type="Product" Version="1.2" xmlns:eg="http://www.esko-graphics.com/EGSchema1_0">
<Comment Name="JobDescription">Test job</Comment>
<Comment Name="Description">Customer request</Comment>
<Comment Name="Papergrade">300g</Comment>
<Comment Name="Printmethod">Offset</Comment>
<Comment Name="Iteration">001</Comment>
</JDF>Thanks in advance!
Copy link to clipboard
Copied
Hi @milevic,
In your JDF document you have set a default namespaces xmlns="http://www.CIP4.org/JDFSchema_1_1". So we will have to set that in our code as well. Try the following
var XMLfile = new File("/Users/manan/Downloads/test.xml");
XMLfile.open('r');
var str = "";
str = XMLfile.read();
XMLfile.close();
var xml = new XML(str)
default xml namespace = "http://www.CIP4.org/JDFSchema_1_1"
for(var i = 0; i < xml.children(0).length(); i++)
alert (xml.children(0)[i])
-Manan
Copy link to clipboard
Copied
Great job. Thanks a lot
Copy link to clipboard
Copied
Dear @Manan Joshi do you have experience with this kind of document listed below? I have some pdf template files and they have some information imortant for me. Could you help me how to read information for vsize hsize from this document, using javascript? Thanks in advanced
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 5.1.2">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:egGr="http://ns.esko-graphics.com/"
xmlns:egInk="http://ns.esko-graphics.com/">
<egGr:nrpages>1</egGr:nrpages>
<egGr:startlogpage>1</egGr:startlogpage>
<egGr:units>mm</egGr:units>
<egGr:vsize>120</egGr:vsize>
<egGr:hsize>120</egGr:hsize>
<egGr:margtop>20</egGr:margtop>
<egGr:margbot>10</egGr:margbot>
<egGr:margleft>20</egGr:margleft>
<egGr:margright>20</egGr:margright>
<egGr:readerspread>False</egGr:readerspread>
<egGr:screenreg>False</egGr:screenreg>
</rdf:Description>
</x:xmpmeta>
Copy link to clipboard
Copied
Dear @Manan Joshi could you help me, how i can read data from this kind of XML? I exported some XML from excel but i am not sure how to read it. For example how i can read "Lenght" from record if i know "Name" of file?
<?xml version="1.0" encoding="utf-8"?>
<data xmlns:xs="http://www.w3.org/2001/XMLSchema">
<record>
<Name>Test.pdf</Name>
<System>Offset</System>
<Size>100</Size>
<Shape>Square</Shape>
<Lenght>158.00</Lenght>
</record>
</data>
Copy link to clipboard
Copied
What have you done so far? Do you have a script to read this that isn't working?
Copy link to clipboard
Copied
I didtn want to open new topic, because it is similar issue. Sorry for time wasting, but I didn't skilled to use the previous solutions on the new xml.
Copy link to clipboard
Copied
The following should work. It will display the name of the nodes with Lenght equal to 158.00. I have used a hardcoded xml string, you can read it from a file as showed in the previous example
var str = '<?xml version="1.0" encoding="utf-8"?>\
<data xmlns:xs = "http://www.w3.org/2001/XMLSchema">\
<record>\
<Name>Test.pdf</Name>\
<System>Offset</System>\
<Size>100</Size>\
<Shape>Square</Shape>\
<Lenght>158.00</Lenght>\
</record>\
<record>\
<Name>Test1.pdf</Name>\
<System>Offset</System>\
<Size>100</Size>\
<Shape>Square</Shape>\
<Lenght>15.00</Lenght>\
</record>\
<record>\
<Name>Test2.pdf</Name>\
<System>Offset</System>\
<Size>100</Size>\
<Shape>Square</Shape>\
<Lenght>18.00</Lenght>\
</record>\
</data >'
var xml = new XML(str)
var result = xml.xpath('//record[Lenght/text()="158.00"]/Name')
for(var i = 0; i < result.children(0).length(); i++)
alert (result.children(0)[i])
-Manan
Copy link to clipboard
Copied
It works, thanks a lot
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more