Skip to main content
Inspiring
October 27, 2020
Answered

Use values from XML into PS

  • October 27, 2020
  • 2 replies
  • 1612 views

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. 

This topic has been closed for replies.
Correct answer Manan Joshi

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

2 replies

Community Expert
October 28, 2020

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

-Manan
milevicAuthor
Inspiring
April 27, 2022

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!

milevicAuthor
Inspiring
May 25, 2022

Great job. Thanks a lot


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>

 

Manan JoshiCommunity ExpertCorrect answer
Community Expert
October 28, 2020

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

-Manan
milevicAuthor
Inspiring
October 28, 2020

@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.

Kukurykus
Legend
October 28, 2020

Everything how to do it is described in 'JAVASCRIPT TOOLS GUIDE'.