I have created an XML file using streamWriter.. Now, i want to remove the line breaks in my XML file.. Is there a way to accomplish this task..?
here's my sample outout
<?xml version="1.0" encoding="utf-8"?>
<!--Arbortext, Inc., 1988-2004, v.4002-->
<!DOCTYPE primary.hierarchy SYSTEM "http://phoenix.roc.westgroup.com/dtd/pax.dtd">
Output should look like this
<?xml version="1.0" encoding="utf-8"?><!--Arbortext, Inc., 1988-2004, v.4002--><!DOCTYPE primary.hierarchy SYSTEM "http://phoenix.roc.westgroup.com/dtd/pax.dtd">
Instead of using StreamWriter you can consider using XmlWriter instead. It has various settings to deal with formatting of XML output.
See XmlWriterSettings for details
BTW. I assume that have you used WriteLine with your StreamWriter approach use Write instead. That should get rid of your line breaks.
Related
I'm new with groovy (a few weeks of experience). Currently I'm trying to process some visual studio .vcproj files using groovy: replacing some paths, that will be found by a regexp patterns. This works fine for me.
To write the changes to the file, I'm using the
XmlUtil.serialize(slurper, writer)
method, where
def writer = new FileWriter(outputFile)
and
def slurper = new XmlSlurper(keepIgnorableWhitespace:true).parse(it)
This also works fine, except one thing.
In the original vcproj file each attribute is in a separate line, like:
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
InheritedPropertySheets="..\..\..\..\Test_Debug.vsprops"
CharacterSet="2"
>
but after calling the serialize() method of the XMLUtil class, the whole output is stored in one line:
<Configurations>
<Configuration Name="Debug|Win32" InheritedPropertySheets="..\..\..\..\Test_Debug.vsprops" OutputDirectory="$(ConfigurationName)" IntermediateDirectory="$(ConfigurationName)" ConfigurationType="1" CharacterSet="2">
for the XMS parser this should be not a problem, but in the postprocessing some perl scripts use this vcproj file and they complain about missing CR/LF within the attribute line.
So is there any easy possibility to configure the XMLslurper or the serialize-class to keep the CR/LF in between of each attributes?
I doubt there is any easy way to format groovy's xml output to that level. Since the output is a valid XML, can't you use somekind of perl XML parser?
Other than that, you can try to match the attributes with a regex and add a line break to them. A very ugly hack:
import groovy.xml.XmlUtil
def original = '''<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
InheritedPropertySheets="..\\..\\..\\..\\Test_Debug.vsprops"
CharacterSet="2"
>
</Configuration>
</Configurations>
'''
parsed = new XmlParser().parseText original
println XmlUtil.serialize(parsed).replaceAll(/[a-zA-Z]*="[^\"]*"/) {
"\n" + it
}
will print:
<?xml
version="1.0"
encoding="UTF-8"?><Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
InheritedPropertySheets="..\..\..\..\Test_Debug.vsprops"
CharacterSet="2"/>
</Configurations>
Below is my xml input:
<projects>
<project>
<name >project1</name>
<language>java</language>
</project>
<project>
<name>project2</name>
<language>mainframe</language>
</project>
</projects>
I want to convert this .xml to .csv file using data-mapper, but unfortunately it doesn't work.
Can anyone send me the sample flow xml for that? It is very important for my project now.
This is a very simple requirement.
You should select your sample XML file in the input section of the datamapper and define a user defined output of type CSV. Once you create a mapping, map the fields from XML (input) to CSV (output). The code would like as below.
//MEL
//START -> DO NOT REMOVE
output.__id = str2long(input.__id);
//END -> DO NOT REMOVE
output.name = input.name;
output.language = input.language;
You can now click on Preview button and run the preview with your sample XML file. Wasn't that easy? Try this and let me if any issues.
I'm getting the above error when I try uploading an auto-suggest xml file to Google's custom site search. I tried trimming the file down to the bear minimum to see if I could isolate the problem but even the following won't upload:
<?xml version="1.0" encoding="utf-8"?>
<Autocompletions>
<Autocompletion term="My term" type="1" />
</Autocompletions>
Am I missing something blindingly obvious?
Kind regards,
Karl
It turns out, despite the Google information to the contrary, that the 'language' attribute is required even if it's value is blank. I added that and the file was successfully imported.
<?xml version="1.0" encoding="utf-8"?>
<Autocompletions>
<Autocompletion term="My term" type="1" language="" />
</Autocompletions>
Make sure that your encoding is UTF-8. Also, term="" i.e. an empty term should not be there in the XML file.
how to add new line in my string resource file?
I already tried \r\n, \n,
,
but it displays exactly what you've indicated, it does not give a new line.
Thanks for your help
When editing resource file in designer press Shift + Enter for new line.
You could, but its a bit indirect. By using
you could add a new line, the only trick here is that & gets converted to in the background. To bring it back to
, all you have to do is open your .resx the file in xml (right-click and open with XML (Text) editor), remove the amp; of the
Before
<data name="HelloWorld" xml:space="preserve">
<value>Hello World</value>
</data>
After
<data name="HelloWorld" xml:space="preserve">
<value>Hello
World</value>
</data>
I am trying to read csv file using smooks1.4 . I want to check the field missmatch , for that in my smooks config file i am using strict="true".but it is throwing
error like this
cvc-complex-type.3.2.2: Attribute
'strict' is not allowed to appear in
element 'csv:listBinding'
this is my smooks-config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.2.xsd">
<csv:reader fields="firstName,lastName,welcome">
<csv:listBinding beanId="customerList" class="example.Customer" strict="true"/>
</csv:reader>
</smooks-resource-list>
My smooks releated Jar files are
milyn-commons-1.4.jar
milyn-smooks-core-1.4.jar
milyn-smooks-csv-1.4.jar
milyn-smooks-javabean-1.4.jar
milyn-smooks-templating-1.4.jar
Help will be appreciated.
You need to use the following URI for the csv namespace:
http://www.milyn.org/xsd/smooks/csv-1.3.xsd
Also, strict is an attribute of csv:reader, not csv:listBinding.