Reputation: 49
I read an XML file:
This is the layout:
<?xml version="1.0" encoding="UTF-8"?>
<RadanCompoundDocument xmlns="http://www.radan.com/ns/rcd">
<RadanAttributes>
<Group class="system" name="File" desc="These attributes are basic properties of the file." ord="1">
<Attr num="101" name="Created" desc="Time file was created." type="t" ord="3" value="2021-05-04T09:39:19+02:00">
<Valid perm="r" />
</Attr>
<Attr num="102" name="Modified" desc="Time file was last modified." type="t" ord="4" value="2021-05-04T09:39:19+02:00">
<Valid perm="r" />
</Attr>
<Attr num="103" name="File size" desc="File size in bytes." type="i" ord="5" value="17832">
<Valid perm="r" />
</Attr>
<Attr num="105" name="Associated files" desc="Associated files." type="s" ord="6">
<Valid perm="h" />
</Attr>
<Attr num="107" name="Extension" desc="File name extension." type="s" ord="2" value="sym">
<Valid perm="r" />
</Attr>
<Attr num="110" name="File name" desc="File name without its extension." type="s" ord="1" value="RingAlu3mm">
<Valid perm="r" />
</Attr>
<Attr num="141" name="Checkout user" desc="The username to which this file is checked out" type="s" ord="7">
<Valid perm="r" />
</Attr>
<Attr num="142" name="Checkout time" desc="The time at which this file was checked out" type="t" ord="8">
<Valid perm="r" />
</Attr>
<Attr num="143" name="Filestore" desc="Radcontrol local filestore path" type="s" ord="9">
<Valid perm="h" />
</Attr>
<Attr num="144" name="Current version" desc='A flag to show (when set to "yes") that this is the current version of a file' type="s" ord="10">
<Valid perm="r" />
</Attr>
</Group>
<Group class="custom" name="Description" desc="Use these attributes to give a description of the file." ord="2">
<Attr num="108" name="Title" desc="The title of this file." type="s" ord="1">
<Valid perm="e" max="80" />
</Attr>
<Attr num="109" name="Comments" desc="Comments about this file." type="s" ord="2">
<Valid perm="e" />
</Attr>
</Group>
<Group class="custom" name="Manufacturing" desc="These attributes are the manufacturing properties of the file." ord="6">
<Attr num="119" name="Material" desc="Material." type="s" ord="1" value="AlMg3 H111">
<Valid perm="e" max="20" />
</Attr>
<Attr num="120" name="Thickness" desc="Thickness." type="r" ord="2" value="3">
<Valid perm="e" min="0" max="99999" />
</Attr>
<Attr num="121" name="Thickness units" desc="Thickness units." type="s" ord="4" value="mm">
<Valid perm="e" expr="mm|in|swg" max="80" />
</Attr>
<Attr num="122" name="Orientation" desc="Allowed nest orientations (1=0°, 2=90°, 3=0/90°, 5=0/180°, 6=90/270°, 7=0/90/180/270°, 8=any)." type="i" ord="6" value="1">
<Valid perm="e" min="1" max="8" />
</Attr>
<Attr num="123" name="Cycle time" desc="Cycle time in minutes." type="r" ord="24">
<Valid perm="e" min="0" />
</Attr>
<Attr num="127" name="Machine" desc="Machine name." type="s" ord="18">
<Valid perm="e" max="80" />
</Attr>
<Attr num="132" name="Mirror" desc="Whether the part can be mirrored in nesting." type="s" ord="7">
<Valid perm="e" expr="yes|no" max="80" />
</Attr>
<Attr num="139" name="STL id" desc="Standard Tool Loading file id." type="i" ord="19" value="23">
<Valid perm="e" min="0" max="99" />
<MC machine="psys_LES_4" value="23" />
</Attr>
<Attr num="145" name="Common cutting" desc="Whether the part is suitable for common line cutting in nesting." type="s" ord="8">
<Valid perm="e" expr="none|unrestricted|same part" max="80" />
</Attr>
<Attr num="146" name="Strategy" desc="Autotooling strategy." type="s" ord="20" value="Nitrogen">
<Valid perm="e" max="60" />
<MC machine="psys_LES_4" value="Nitrogen" />
</Attr>
<Attr num="160" name="Picking cluster" desc="Whether to nest the part in clusters that are suitable for a part picker." type="s" ord="9">
<Valid perm="e" expr="yes|no" max="80" />
</Attr>
<Attr num="164" name="Weight" desc="The weight of the part in lb or kg, depending on the geometry units attribute value." type="r" ord="5" value="0.01923785677">
<Valid perm="e" min="0" />
</Attr>
<Attr num="181" name="Maximum CC group" desc="The maximum number of parts in a common cut group." type="i" ord="10">
<Valid perm="e" min="2" max="999999" />
</Attr>
<Attr num="186" name="Workflow status" desc="The workflow status of this file." type="s" ord="27" value="1 - OK">
<Valid perm="e" max="80" />
<MC machine="psys_LES_4" value="1 - OK" />
</Attr>
<Attr num="196" name="Kit of parts" desc="Symbol is a kit of parts." type="s" ord="11">
<Valid perm="e" expr="yes|no" max="80" />
</Attr>
</Group>
</Group>
</RadanAttributes>
</RadanCompoundDocument>
I want to get the value of the object with name="Strategy" so I do this:
const xml = fs.readFileSync(path)
const result = {}
let symXML = ''
parser.parseString(xml, function(err, res) {
if (err) {
console.error('xml2js.parseString: Error occurred: ', err)
} else {
symXML = res
}
})
const manufacturing = symXML.RadanCompoundDocument.RadanAttributes.Group.find(
(el) => el.$.name === 'Manufacturing'
)
what gives me the manufacturing section as an object
{
'$': {
class: 'custom',
name: 'Manufacturing',
desc: 'These attributes are the manufacturing properties of the file.',
ord: '6'
},
Attr: [
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object], MC: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object], MC: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object] },
{ '$': [Object], Valid: [Object], MC: [Object] },
{ '$': [Object], Valid: [Object] }
]
}
inside the array Attr it looks like this:
{
'$': {
num: '165',
name: 'Bounding box X',
desc: "The maximum extent of the symbol's geometry in the X direction.",
type: 'r',
ord: '2',
value: '75'
},
Valid: { '$': { perm: 'e', min: '0' } }
}
and its here that i need the value of property 'value'
This won't work:
const result = {}
result.Strategy = manufacturing.Attr.find((el) => el.$.name === 'Strategy').$.value
Why does Array.prototype.find().$.value not give me a result?
This works:
result.Strategy = manufacturing.Attr.find((el) => el.$.name === 'Strategy').$.value
result.Strategy = result.Strategy.$.value
But that's just not right.
Can someone please help me?
Upvotes: 0
Views: 60
Reputation: 49
I found it.
Array.prototype.find(), The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
Array.prototype.filter() The filter() method creates a new array with all elements that pass the test implemented by the provided function.
.filter returns a new array, so take the fist element [0], and then navigate in it as an object .$.value so:
result.Strategy = manufacturing.Attr.filter((el) => el.$.name === 'Strategy')[0].$.value
Upvotes: 1