Java:如何将变量数据从XML发送到java [英] Java :how to send the variable data from XML to java
问题描述
抱歉,我是java新手。目前我开发的工具发送电子邮件,但数据,如(来自,主题,消息)我把它作为xml文件,我可以随时更改它。问题是我需要如何将数据从xml发送到java变量?
$ c $ b在c#中:我可以这样做,我可以链接变量。在java中我不确定并且有错误eElement无法解析。
String sto =(eElement.getElementsByTagName(to)。item(0).getTextContent( ));
i想要链接此变量中的数据
来自的字符串=测试;
String to =test;
String subject =test;
String msg =test;
对不起,如果这个问题看起来很愚蠢或让你们混淆的话。
谢谢
我尝试过:
< pre> public static void main(字符串 [] args)
{
// *************************启动邮件主要***************** ****************** 88
System.out.println( 启动应用程序);
ApplicationContext context = new ClassPathXmlApplicationContext( 弹簧-Mail.xml跨度>);
MailMail mm =(MailMail)context.getBean( mailMail) ;
// **************** ************************************************** *******
字符串 from = 测试跨度>;
String to = test跨度>;
字符串 subject = test跨度>;
字符串 msg = test跨度>;
字符串 sto =(eElement.getElementsByTagName( to)。item( 0 )。getTextContent());
mm.sendMail(from,to,subject,msg);
// **************** ****获取数据**********************
尝试 {
文件fXmlFile = 新文件( < span class =code-string> C:\\Users \\s012160 \\ Eclipse \\Spring-Myproject \\AA-Mail-Draft3 \\SpringExample\\\ \\src\\main\\resources\\mailDetail.xml跨度>);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement()。normalize();
System.out.println( 根元素: + doc。 。getDocumentElement()getNodeName());
System.out.println( ----------- -----------------跨度>);
NodeList nList = doc.getElementsByTagName( send); // 获取元素特定ID
for ( int temp = 0 ; temp< nList.getLength() ; temp ++){
Node nNode = nList.item(temp);
System.out.println( \ n当前元素: + nNode.getNodeName());
if (nNode.getNodeType()== Node.ELEMENT_NODE){
元素eElement =(元素) nNode;
System.out.println( To: + eElement.getElementsByTagName ( to)。item( 0 )getTextContent())。
System.out.println( From: + eElement.getElementsByTagName( from)。item( 0 )。getTextContent ());
System.out.println( 主题: + eElement.getElementsByTagName( subject)。item( 0 )。getTextContent ());
System.out.println( 消息: + eElement.getElementsByTagName( msg)。item( 0 )。getTextContent ());
}
}
} catch (例外e){
e.printStackTrace();
}
参见 xml java - Google搜索 [ ^ ]。
Sorry ,im new in java. Currently i developing the tools to send the email, but the data such as (to, from, subject, message) i put it as xml file, where i can change it anytime. the question is how i need to send the data from xml to java variable ?
in c#: i can do like this, where i can link the variable. in java im not sure and have an error "eElement cannot be resolve".
String sto = (eElement.getElementsByTagName("to").item(0).getTextContent());
i want to link the data in this variable
String from = "test"; String to = "test"; String subject = "test"; String msg = "test";
sorry if this question look stupid or make you guys confuse.
thank you
What I have tried:
<pre> public static void main( String[] args )
{
//*************************start mail main ***********************************88
System.out.println("Start Application");
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");
MailMail mm = (MailMail) context.getBean("mailMail");
//*************************************************************************
String from = "test";
String to = "test";
String subject = "test";
String msg = "test";
String sto = (eElement.getElementsByTagName("to").item(0).getTextContent());
mm.sendMail(from, to, subject, msg);
//********************fetch data**********************
try {
File fXmlFile = new File("C:\\Users\\s012160\\Eclipse\\Spring-Myproject\\AA-Mail-Draft3\\SpringExample\\src\\main\\resources\\mailDetail.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
System.out.println("----------------------------");
NodeList nList = doc.getElementsByTagName("send"); //get the element specific ID
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("To : " + eElement.getElementsByTagName("to").item(0).getTextContent());
System.out.println("From : " + eElement.getElementsByTagName("from").item(0).getTextContent());
System.out.println("Subject : " + eElement.getElementsByTagName("subject").item(0).getTextContent());
System.out.println("Msg : " + eElement.getElementsByTagName("msg").item(0).getTextContent());
}
}
}catch(Exception e) {
e.printStackTrace();
}
See xml java - Google Search[^].
这篇关于Java:如何将变量数据从XML发送到java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!