如何使用java更改Xml中的数据?
假设我有一个xml,如下所示:如何使用java更改Xml中的数据?,java,xml,jaxb,Java,Xml,Jaxb,假设我有一个xml,如下所示: <alerts> <fullName>email_alert_campaign</fullName> <description>email alert campaign</description> <recipients> <recipient>abc_puv@xyz.com</recipient> <typ
<alerts>
<fullName>email_alert_campaign</fullName>
<description>email alert campaign</description>
<recipients>
<recipient>abc_puv@xyz.com</recipient>
<type>user</type>
</recipients>
<senderType>CurrentUser</senderType>
</alerts>
<tasks>
<fullName>Task_on_completing_a_campaign</fullName>
<assignedTo>abc_puv@xyz.com</assignedTo>
<subject>Task on completing a campaign</subject>
</tasks>
电子邮件提醒活动
电子邮件提醒活动
abc_puv@xyz.com
用户
当前用户
关于完成活动的任务
abc_puv@xyz.com
完成活动的任务
在xml文件中,我有像“abc”这样的数据_puv@xyz.com需要使用java将其替换为“XYZ”我必须编写一个java代码,它可以在100个xml中搜索并相应地替换为数据。请尝试代码
test.xml
<?xml version="1.0"?>
<root>
<alerts>
<fullName>email_alert_campaign</fullName>
<description>email alert campaign</description>
<recipients>
<recipient>abc_puv@xyz.com</recipient>
<type>user</type>
</recipients>
<senderType>CurrentUser</senderType>
</alerts>
<tasks>
<fullName>Task_on_completing_a_campaign</fullName>
<assignedTo>abc_puv@xyz.com</assignedTo>
<subject>Task on completing a campaign</subject>
</tasks>
</root>
public class XMLParsing {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
File fXmlFile = new File("D:/test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
// optional, but recommended
// read this -
// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath
.compile("//assignedTo");
Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
System.out.println("Root element :"
+ node.getTextContent());
node.setTextContent("abc_puv@efg.com");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
}
}
电子邮件提醒活动
电子邮件提醒活动
abc_puv@xyz.com
用户
当前用户
关于完成活动的任务
abc_puv@xyz.com
完成活动的任务
公共类XML解析{
公共静态void main(字符串[]args)引发ParserConfiguration异常,
SAXException、IOException、XPathExpressionException、TransformerFactoryConfigurationError、TransformerException{
File fXmlFile=新文件(“D:/test.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
//可选,但推荐
//读这个-
// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
XPath=XPathFactory.newInstance().newXPath();
XPathExpression expr=xpath
.compile(“//assignedTo”);
Node Node=(Node)expr.evaluate(doc,XPathConstants.Node);
System.out.println(“根元素:”
+node.getTextContent());
node.setTextContent(“abc_puv@efg.com");
Transformer Transformer=TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
//使用文件对象初始化StreamResult以保存到文件
StreamResult=新的StreamResult(新的StringWriter());
DOMSource=新的DOMSource(doc);
变换(源、结果);
字符串xmlString=result.getWriter().toString();
System.out.println(xmlString);
}
}
如果您(根据标签)希望使用JAXB执行此操作,步骤如下:
例如,编写一个XSLT转换
<xsl:stylesheet ...>
<xsl:template match="*">
<xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>
<xsl:template match="recipient|assignedTo">
<xsl:copy><xsl:value-of select="replace(., 'abc_puv@xyz.com', 'XYZ')"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
然后(例如,通过使用JAXP API)将此转换应用于每个XML文档。如何将更改后的数据持久化为XML?无法将更改的数据保存在xml中