I am really new to XML and JDOM so I have a noob question, sorry for that. I have a XML file and I want to insert value into it. My XML file is like that;
<?xml version="1.0"?>
<message>
<header>
<messageType> </messageType>
<sendFrom> </sendFrom>
<HostName> </HostName>
<sendTo> </sendTo>
<receiverName> </receiverName>
<date> </date>
</header>
<body>
</body>
</message>
So what I want is for example is to add value between <sendTo> </sendTo>
and also I want to add <A>
data </A>
between <body> </body>
. Can you please tell me how to do that ?
Thanks a lot.
If you use dom,you can do it as follows;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(inputFile);
Node messageType= doc.getElementsByTagName("messageType").item(0);//zero tells the order in the xml
messageType.setTextContent("SMS");
Zander
I'd recommend using XStream for XML handling. Here, is the link to a 2 minute tutorial: http://x-stream.github.io/tutorial.html
来源:https://stackoverflow.com/questions/7610894/how-to-insert-value-into-xml