Avatar billede fredand Forsker
08. april 2005 - 15:21 Der er 7 kommentarer og
1 løsning

How to Insert Values into a XML doc?

Hello!
Suppose that I got a xml-pattern/structure like this:

<?xml version="1.0" encoding="UTF-8"?>
<protocol>
    <type></type>
    <version></version>
    <commands></commands>
</protocol>

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>

Any ideas?

Best regards
Fredrik
Avatar billede arne_v Ekspert
08. april 2005 - 15:26 #1
Avatar billede arne_v Ekspert
08. april 2005 - 15:27 #2
Not quite as easy as what you outline but possible.

The only easier way would be to do a XPATH selectNode and append to that.
Avatar billede fredand Forsker
08. april 2005 - 16:14 #3
Hello Arne!

Thanks for your reply!

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:/...

Best regards
Fredrik
Avatar billede arne_v Ekspert
08. april 2005 - 19:19 #4
doc.setDocType(new DocType(elm, pubid, id));

would be my guess
Avatar billede fredand Forsker
08. april 2005 - 21:31 #5
Yes!

It works perfect!

May I ask for a little "svar"?

Best regards mate!

Fredrik
Avatar billede arne_v Ekspert
08. april 2005 - 21:32 #6
svar
Avatar billede arne_v Ekspert
08. april 2005 - 21:53 #7
If I should illustarte the XPath technique:

package april;

import java.io.IOException;
import java.io.StringReader;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;

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>
Avatar billede fredand Forsker
10. april 2005 - 16:34 #8
Thank you a lot!

That was a great explanation mate!

Best regards
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester