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
October 28, 2020

Thanks a lot @Manan Joshi @Kukurykus 

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