Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Use values from XML into PS

Engaged ,
Oct 27, 2020 Oct 27, 2020

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. 

TOPICS
Actions and scripting
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 27, 2020 Oct 27, 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 
...
Translate
Adobe
Community Expert ,
Oct 27, 2020 Oct 27, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 28, 2020 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 28, 2020 Oct 28, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 28, 2020 Oct 28, 2020

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2020 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 28, 2020 Oct 28, 2020

Thanks a lot @Manan Joshi @Kukurykus 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2022 Apr 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2022 Apr 27, 2022

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2022 Apr 27, 2022

Great job. Thanks a lot

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 25, 2022 May 25, 2022

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>

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 29, 2022 Aug 29, 2022

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>

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 30, 2022 Aug 30, 2022

What have you done so far? Do you have a script to read this that isn't working?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 31, 2022 Aug 31, 2022
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 30, 2022 Aug 30, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 31, 2022 Aug 31, 2022

It works, thanks a lot

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines