user2348157
user2348157

Reputation: 103

Java convert XML to YAML

I need to convert a XML File to YAML. I could not find anything helpful on Google. Is there any similar API like the json in java to convert a XML file to YAML?

Upvotes: 1

Views: 6331

Answers (3)

Valentyn Kolesnikov
Valentyn Kolesnikov

Reputation: 2097

You may combine underscore-java and snakeyaml libraries.

Steps:

  • Read xml file to map.
  • Generate yml file from map.

    import com.github.underscore.lodash.Xml;
    import java.io.StringWriter;
    import org.yaml.snakeyaml.Yaml;
    
    Object result = Xml.fromXml(xml);
    Yaml yaml = new Yaml();
    StringWriter stringWriter = new StringWriter();
    yaml.dump(result, stringWriter);
    String yaml = stringWriter.toString();
    

Upvotes: 0

Hana Bzh
Hana Bzh

Reputation: 2260

Other than json, you can use TestNG to convert XML to YAML,

http://testng.org/javadoc/org/testng/Converter.html

http://www.infoq.com/news/2011/03/testng-60

Upvotes: 0

Benjamin M
Benjamin M

Reputation: 24527

You could use Jackson, it supports XML and YAML (and also JSON, CSV and more).

https://github.com/FasterXML/jackson-dataformat-xml

https://github.com/FasterXML/jackson-dataformat-yaml

Or if it's more easy to understand for you, take two steps: XML -> JSON , JSON -> YAML. Because there are lot's of tutorials for XML -> JSON and YAML is pretty similar to JSON.

Upvotes: 1

Related Questions