find and replace an int in xml file - java

I have a issue where i need to find and replace an int within a xml file.
Here is an example file:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Element time="0.00000" num="10723465" />
<Element time="7.98000" num="10028736" />
<Element time="8.40000" num="94123576" />
</data>
I want to find and replace the "num" attribute. I have been able to do this with the DOM factory however it doesn't keep the order of the attributes. There must be a simpler way to find and replace the num. any help would be great :)

Advice 1: you should use XML library to parse, nothing else, or it would be a pain.
Information 2: order doesnt matter in XML for attributes. Then you should forget this problem:
See this with much more details
Order of XML attributes after DOM processing
Alternative: use Regex (it can work for very simple XML). example in the link before

Related

how can i get a matching string from a xml file using <getxmlproperties> tag using ant and replace the matched string in other xml file

My requirement is to match following tag in data.xml file and replace the content in display.xml file using in ant
data.xml
--------
<data>123456789</data>
display.xml
-----------
<data>abcdefg</data
I need to match the content in data.xml file and replace the it in display.xml file.
my final output should be like:
data.xml
--------
<data>123456789</data>
display.xml
-----------
<data>123456789</data>
How can i solve this Issue? Thanks in advance
I did not find any Ant task named GetXmlProperties, but I think you may have thought about this one, XmlProperty, which composes a sequence of properties from parsing an XML document.
Two ways, to achieve what you want, come to my mind (there may be more):
The most primitive would be to just use the XmlProperty task to retreive the value in question and use a crude Replace task on the destination file, doing a simple string replace, by handling the destination as a plain text file, instead of caring for the XML logic in it. However, doing string match and replace with XML data is no fun and error prone.
Thus I propose a second approach, which is to use the XmlTask, as per the following example. Adding a prefix xml to our newly parsed properties makes it easier to distinguish them from the rest. For demo purposes we also let the build process log all the new properties under the xml prefix to the console by using the EchoProperties task.
<project name="SO63816092" default="default">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
<xmlproperty file="data.xml" prefix="xml" />
<echoproperties prefix="xml"/>
<target name="default">
<xmltask source="display.xml" dest="display.xml" failWithoutMatch="true">
<replace path="//data/text()" withText="${xml.data}" />
</xmltask>
</target>
</project>
Using the second approach, while using the following input file data.xml:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
and the following destination file display.xml:
<?xml version="1.0" encoding="UTF-8"?>
<data>abcdefg</data>
I can successfully accomplish what you ask for and get display.xml to become:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
Just remember to place the XmlTask Java jar in the current classpath for your Ant process and note, that you may need to change the XPath expression, we use, //data/text(), to something more refined, should your document structure demand it, because the way, it is now, it would replace the value for all data elements it finds, throughout the whole display.xml document.

How do I find all xml elements of a specific type using xpath?

I have an XSD that defines a complexType (say 'FooType'), and several named instances of this type scattered throughout the same XSD, like:
<sequence>
<element name="A" type="tns:FooType"/>
<element name="B" type="tns:FooType"/>
</sequence>
When working with an XML file derived from the XSD, I want to find all element nodes that of the type "tns:FooType". I think this is possible using XPath with the element(*, "FooType) method, but I can't find any examples of this so don't know what the syntax would look like. I'm hoping to use this with the Java dom4j selectNodes() method.
You need an XPath 2.0 implementation. DOM4J is 1.0 only, and so is javax.xml.xpath. Saxon provides 2.0, but I believe this specific capability is not part of the open source edition.
Try this:
List list = document.selectNodes( "/sequence/element[#type='tns:FooType']" );
If you do not know the prefix you could use substring-before() on the name and check if there is a result:
/sequence/element[substring-before(#type,":FooType")]

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.

How to split xml to header and items using smooks?

I have a xml file roughly like this:
<batch>
<header>
<headerStuff />
</header>
<contents>
<timestamp />
<invoices>
<invoice>
<invoiceStuff />
</invoice>
<!-- Insert 1000 invoice elements here -->
</invoices>
</contents>
</batch>
I would like to split that file to 1000 files with the same headerStuff and only one invoice. Smooks documentation is very proud of the possibilities of transformations, but unfortunately I don't want to do those.
The only way I've figured how to do this is to repeat the whole structure in freemarker. But that feels like repeating the structure unnecessarily. The header has like 30 different tags so there would be lots of work involved also.
What I currently have is this:
<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:calc="http://www.milyn.org/xsd/smooks/calc-1.1.xsd"
xmlns:frag="http://www.milyn.org/xsd/smooks/fragment-routing-1.2.xsd"
xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
</params>
<frag:serialize fragment="INVOICE" bindTo="invoiceBean" />
<calc:counter countOnElement="INVOICE" beanId="split_calc" start="1" />
<file:outputStream openOnElement="INVOICE" resourceName="invoiceSplitStream">
<file:fileNamePattern>invoice-${split_calc}.xml</file:fileNamePattern>
<file:destinationDirectoryPattern>target/invoices</file:destinationDirectoryPattern>
<file:highWaterMark mark="10"/>
</file:outputStream>
<resource-config selector="INVOICE">
<resource>org.milyn.routing.io.OutputStreamRouter</resource>
<param name="beanId">invoiceBean</param>
<param name="resourceName">invoiceSplitStream</param>
<param name="visitAfter">true</param>
</resource-config>
</smooks-resource-list>
That creates files for each invoice tag, but I don't know how to continue from there to get the header also in the file.
EDIT:
The solution has to use Smooks. We use it in an application as a generic splitter and just create different smooks configuration files for different types of input files.
I just started with Smooks myself. However... your problem sounds identical to this: http://www.smooks.org/mediawiki/index.php?title=V1.5:Smooks_v1.5_User_Guide#Routing_to_File
You will have to provide the output FTL format in whole, that's the downside of using a general purpose tool I guess. Data mapping often includes a lot of what feels like redundancy, one way around this is to leverage convention but that has to be built into the framework.
I don't know smooks, but the simplest solution (with poor performance) would be (to create the Nth file):
copy the whole xml structure
delete all the invoice tags but the Nth one
I don't know how to do that in smooks, that only an idea. In this case you don't need to duplicate the structure of the xml in a freemarker template.

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