How should I do to insert text values into type, version and command in an easy way?
I use JDOM but I have not found any easy way to do that.
To me it look like it would be nice with something like this:
XmlDocument xmlDoc = new XmlDocument("the_xml_pattern.xml"); xmlDoc.setTextValue("type" "my_type_value"); xmlDoc.setTextValue("version" "my_version_value");
And perhaps: Node node = new Node("<command>CTRL_V</command>"); xmlDoc.setChildValue("commands" node);
And the result would look like: <?xml version="1.0" encoding="UTF-8"?> <protocol> <type>my_type_value</type> <version>my_version_value</version> <commands> <command>CTRL_V</command> </commands> </protocol>
I found this which did it quite easy! Element type = new Element("type"); type.addContent("CTTP"); Element version = new Element("version"); version.addContent("1.0"); Element command = new Element("command"); command.addContent("MESS");
Element protocol = new Element("protocol"); protocol.addContent(type); protocol.addContent(version); protocol.addContent(command);
Now i just need to figure out how to set a DTD path like: <!DOCTYPE message PUBLIC "foo" "http:/...
public class JDomXPath { private static final String template = "<protocol>" + "<type></type>" + "<version></version>" + "<commands></commands>" + "</protocol>"; public static void fillout(Document doc, String tag, String value) throws JDOMException { Element elm = (Element) XPath.selectSingleNode(doc, "protocol/" + tag); elm.setText(value); } public static void main(String[] args) throws JDOMException, IOException { SAXBuilder b = new SAXBuilder(); Document doc = b.build(new StringReader(template)); fillout(doc, "type", "Dette er en type"); fillout(doc, "version", "Dette er en version"); fillout(doc, "commands", "Dette er en commands"); XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat()); System.out.println(fmt.outputString(doc)); } }
output:
<?xml version="1.0" encoding="UTF-8"?> <protocol> <type>Dette er en type</type> <version>Dette er en version</version> <commands>Dette er en commands</commands> </protocol>
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.