Skip to content
Michiel TJampens edited this page Mar 28, 2021 · 1 revision

XMLfab applies XMLtools to provide an interface that allows building a XML file.

// First either connect to a new file or provide path to a new one,
XMLfab.withRoot( Paths.get(xmlPath),"root","subroot") //or a Document instead of a path and any level of root
      .build(); // build the document/file

This would be the result:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
  <subroot/>
</root>

Or a more elaborate example:

XMLfab.withRoot( Paths.get(xmlPath),"root","subroot")
      .addChild("branch") // add one or multiple in one go
      .addParent("heavybranch") // Or a node that can attach children
        .addchild("twig")      // another child without value 
        .addChild("twig","leftside") // or with a textnode
            .attr("leaves",5).attr("color","red")   // add one or more attributes to the last added node (parent or child)
    .build();

This would be the result:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
  <subroot>
    <branch></branch>
      <heavybranch>
        <twig/>
        <twig leaves="5" color="red">lefside</twig>
      </heavybranch>
  </subroot>
</root>

If you want to go a level lower...

XMLfab.withRoot( Paths.get("testing.xml"), "root","subroot")
      .addParent("heavybranch")
        .addChild("twig")
            .down()
            .addChild("leaf")
                .down()
                .addChild("bug")
                .addChild("bud")
            .back()
            .addChild("leaf2")
        .back()
        .addParent("twig2")
            .addChild("leafy")
      .build();

This results in:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
  <subroot>
    <heavybranch>
      <twig>
        <leaf>
          <bug/>
          <bud/>
        </leaf>
        <leaf2/>
      </twig>
    </heavybranch>
    <twig2>
      <leafy/>
    </twig2>
  </subroot>
</root>

Or add comments

XMLfab.withRoot( Paths.get("testing.xml"), "root","subroot")
        .comment("Really heavy!")
        .addParent("heavybranch")
            .addChild("twig")
                .comment("Information about the twig goes here")
      .build();
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
  <subroot>
    <!--Really heavy!-->
    <heavybranch>
      <twig>
        <!--Information about the twig goes here-->
      </twig>
    </heavybranch>
  </subroot>
</root>

Or to alter that last file

XMLfab.withRoot( Paths.get("testing.xml"), "root","subroot")
        .selectParent("heavybranch")
            .alterChild("twig", "somevalue!").attr("leaves",10)
      .build();
// Or if the file contains multple heavybranch nodes
XMLfab.withRoot( Paths.get("testing.xml"), "root","subroot")
        .selectParent("heavybranch","weight","10kg")  // A heavybranch node with a weight attribute with value 10kg
            .alterChild("twig", "somevalue!").attr("leaves",10) // If the earlier branch wasn't found, this is ignored?
      .build();
Clone this wiki locally