0

假设我有一个 xml 如下:

<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>

在一个xml文件中,我有像“abc_puv@xyz.com”这样的数据需要使用java替换为“XYZ”我必须编写一个java代码,它可以在100个xml中搜索并相应地替换为数据。

4

3 回答 3

0

如果您(根据标签)希望使用 JAXB 执行此操作,则步骤如下:

  1. 必要时使用标准 JAXB 注释将您的对象模型映射到 XML 结构。
  2. 将 XML 解组到您的对象模型中。
  3. 如果需要更改,请更新值。
  4. 如果模型更改,则将其封送回 XML。
于 2013-06-12T12:11:24.740 回答
0

Write an XSLT transformation, for example

<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>

Then (e.g. by using the JAXP API) apply this transformation to each of your XML documents.

于 2013-06-12T15:14:56.420 回答
0

请尝试代码 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);
    }
}
于 2013-06-12T11:57:15.803 回答