Fetch value of a node's attribute in XML - java

I have an XML document, a snippet of which is below:
<item name="entryDataItem" type="dataView" caption="__entry_data_item" id="208" formItem="selectbox">
<properties>
<property name="caption" value="Task Type"/>
<property name="name" value="task_type"/>
</properties>
</item>
<item name="entryDataItem" type="dataView" caption="__entry_data_item" id="211" formItem="text">
<properties>
<property name="caption" value="Time Spent (Min)"/>
<property name="name" value="time_spent_min"/>
</properties>
</item>
etc.
There are other "properties" as well. The value of "task_type" as well as all other properties are stored in database. When I iterate through the document, all the properties are fetched and a document is prepared with "caption" and "value" from the XML. The problem is , all the "properties" are printed. But I want contents of the document to be based on the "task_type"; say when the task_type value is TESTING", then only task_type "ESTIMATION" would be printed and nothing else.
I understand I have to put a check just before where the document is being prepared.
My question is: How do I put the condition when I only have "task_type" in the XML and not a value(eg."TESTING") directly within the XML document?
My code is a simple JAVA code.
Any help as to how this can be achived is much appreciated.

In my opinion, XPath is the bee's knees when it comes to going after specific items in a XML document. This article might be of some use if it's something you want to persue.

Related

Spring Batch Xml Item Read

I'm working with Spring Batch.
I think I already know how I can parse good-looking xml and binding object.
And I stuck with this problem.
<parent>
<child>
<key name="xxx">
<value>val</value>
</key>
<key name="yyy">
<value>val</value>
</key>
</child>
</parent>
How can I parse xml items for above xml to children defined like this?
class Child {
private String xxx;
private String yyy;
}
I know how I can parse when it looks like this.
<parent>
<child>
<xxx>val</xxx>
<yyy>val</yyy>
</child>
</parent>
Is there any XPath like mappings with Spring-Batch's XML item reader?
Use a Jaxb2Unmarshaller.
Check aroudn for a jaxb solution: it's full around the net.
In the worst case I remember you can write your own node parser and manage this form of your child tag.
I'm sorry but I can't add code because I'm not at work.
Hope can be help

Using XPath in java with if statements?

Im trying to merge & evaluate xml using xpath in Java.
I know its xpath 1.0, and 2.0 added if statements.
Im still trying to resolve the issue, hopefuly with your help.
so, 2 files:
1. data.xml
2. view.xml
view.xml contains attributes with xpath expressions, for example:
<item name="/names/name" .... />
I wish to read the entire view.xml as dom-tree, then select all the attributes with xpath expressions and evaluate them.
in this case, with a data.xml such as:
<names>
<name>Bon</name>
</names>
I expect my resulting dom-tree to be:
<item name="Bon" .... />
so far, that isnt too problematic.
The issue comes where I want to be more demanding, such as "if /names/name="Bon" return "name exists"
so that the result would be
<item name="name exists" .... />
What possible ways I have to do such a thing with Java xpath engine (or maybe something else?)

Map from XML using rest-assured

I'm trying to set up an integration test using rest-assured. In one of my test cases I have to validate some properties of an XML file with rest-assured's XmlPath which seems to use Groovy's GPath.
I have an XML document with the following structure (the ids are unique):
<rootelement>
<someelement id="1234" type="a">
<property key="hello" value="world" />
<property key="name" value="a name" />
<property key="status" value="new" />
<child target="645823" type="a" />
<child target="7482" type="b" />
<child target="8942" type="c">
<property key="pro" value="yes" />
</child>
</someelement>
<someelement>
...
</someelement>
<rootelement>
Ideally, given a someelement id, I want to get a map of it's properties, i.e. assuming the given someelement id is 1234 I'd like to get a map that looks like the following:
{"hello": "world", "name": "a name", "status": "new"}. How would I do this? I know that there's a getMap method in XmlPath, but I couldn't figure out which expression I'd have to use.
If it's not possible to get the properties as a map, I would be content with getting a list of the keys and a list for the values. Again, I don't know which expression I have to use. I tried something like that:
xmlPath.getList("**.find {it.#id = '1234'}.property.#key", String.class)
However, it doesn't find anything.
You can do this with Groovy (assuming xml is a String containing your xml)
def map = new XmlParser().parseText( xml )
.someelement
.find { it.#id == '1234' }
.property
.collectEntries { [ it.#key, it.#value ] }
assert map == [ hello:'world', name:'a name', status:'new' ]
Never used rest-assured, so I can't be much help in that direction though :-(

Tool or schema that describes Adobe Premiere Pro

I have some data outside that i would like to use to create certain edit effects in Adobe Premiere Pro. Rather than editing by hand adding keyframes over time with my data i would like to automate this and write or use a tool to create an XML fragment and update the project file.
I have looked at the XML and some properties are evident. However most data is hidden away as comma separated values, which of course means theres no self documenting tag name. I am therefore after a schema or documentation that describes the format of some or all effects.
<VideoComponentParam ObjectID="48" ClassID="fe47129e-6c94-4fc0-95d5-c056a517aaf3" Version="8">
<Node Version="1">
<Properties Version="1">
<ECP.Angle.Expanded>false</ECP.Angle.Expanded>
<ECW.Parameter.VelocityHeight>54</ECW.Parameter.VelocityHeight>
</Properties>
</Node>
<RangeLocked>false</RangeLocked>
<ParameterID>5</ParameterID>
<CurrentValue>0.</CurrentValue>
<UnitsString></UnitsString>
<UpperBound>32767.</UpperBound>
<LowerBound>-32768.</LowerBound>
<Keyframes>913287043468800,270.,0,0,0,0.166667,-32.4615,0.166667;914685944772533,91.230003356934,0,0,-32.4615,0.166667,14.5418,0.166667;916236575654400,180.,0,0,14.5418,0.166667,-11.4292,0.166667;920237090572800,0.,0,0,-11.4292,0.166667,0,0.166667;</Keyframes>
<StartKeyframe>-91445760000000000,0.,0,0,0,0,0,0</StartKeyframe>
<ParameterControlType>3</ParameterControlType>
<DiscontinuousInterpolate>false</DiscontinuousInterpolate>
<IsLocked>false</IsLocked>
<IsTimeVarying>true</IsTimeVarying>
<Name>Rotation</Name>
</VideoComponentParam>
The interesting tag is of course the Keyframes, which appears to include the keyframe, rotation degrees and some other numbers. I havent yet decyphered the first value which is obviously the timestamp.
Any help in undetrstnding the XML is appreciated.
ADOBE FORUMS
http://forums.adobe.com/thread/962485
Todd_Kopriva, 14-Feb-2012 00:18 in reply to br4ime Report No, there is not any public documentation about the structure of the
Premiere Pro project file format. Was this helpful? Yes No
FINAL CUT PRO XML
I have exported a simple project to Final Cut Pro XML and it appears to be functional but in the above case about rotation over several keyframes, the FCP file has far fewer values.
<parameter authoringApp="PremierePro">
<parameterid>rotation</parameterid>
<name>Rotation</name>
<valuemin>-8640</valuemin>
<valuemax>8640</valuemax>
<value>0</value>
<keyframe>
<when>107634</when>
<value>123</value>
</keyframe>
<keyframe>
<when>107784</when>
<value>124</value>
</keyframe>
<keyframe>
<when>107934</when>
<value>126</value>
</keyframe>
</parameter>
Here is full description of Final Cut XML format. it is same as Premiere XML.
Go to developer.apple.com and find the document that describes FinalCutPro XML format, it's exact the same as Premiere pro XML. The structure is simple, for example this is sequence block format:
<?xml version="1.0" encoding="UTF-8"?>
<xmeml version="3">
<sequence>
<name>Sequence 1</name>
<duration></duration>
<rate>. . .</rate>
<timecode>. . .</timecode>
<media>
<video>
<format></format>
<track></track>
</video>
<audio>
<format></format>
<outputs></outputs>
<track></track>
<track></track>
</audio>
</media>
</sequence>
</xmeml>
direct link is:
https://developer.apple.com/appleapplications/download/FinalCutPro_XML.pdf
THe best solution is to make changes and study the file differences with your favourite diff'ing tool. Its not terribly difficult to understand small fragments and hand edit the XML. Naturally its a pain to make a change and reload the project file and observe the changes, buts its doable.

Java replace in XML-file

I have created my own XML-file on my Android phone, which looks similar to this
<?xml version="1.0" encoding="utf-8" ?>
<backlogs>
<issue id="1">
<backlog id="0" name="Linux" swid="100" />
<backlog id="0" name="Project Management" swid="101" />
</issue>
<issue id="2">
<backlog id="0" name="Tests" swid="110" />
<backlog id="0" name="Online test" swid="111" />
<backlog id="0" name="Test build" swid="112" />
<backlog id="0" name="Update" swid="113" />
</issue>
</backlogs>
I have then converted it into a String to replace inside the string using Regular Expression, but I have a problem with the Regular Expression. The Regular Expression I just created looks like this
([\n\r]*)<(.*)issue(.*)1(.*)([\n\r]*)(.*)([\n\r]*)(.*)([\n\r]*)(.*)<(.*)/(.*)issue(.*)
I need to replace the specific issue-tag (located with the specific ID) with another issue-tag in another String
The Regular Expression works fine for the tag with ID 1, but not with ID 2 as there is another amount of tags, but is there any way to get around the use of amount?
I hope you understand my question
I finally found a solution for my question, which is
([\n\r]*)<(.*)issue(.*)1[\S\s]*?<(.*)/(.*)issue(.*)
Do not use regex. Please. Use XML parser.
Do you know what is the highest voted SO answer
Use a SAX (or StAX) parser and writer at the same time.
As you read one event, detect whether to write the same event type to the writer without modification, or to do some modifications in the state you are currently in - like swapping an element name or attribute value. This will handle an unlimited amount of elements at the expense of CPU usage; in general it will be pretty light-weight.

Categories

Resources