前言:需求格式各样,要是人力能解决问题的情况下,要机器干嘛?所以脚本处理本文内容势在必行!必须要学会处理这,提高生产力!人类伟大就是会利用各种工具。ChatGPT就是现代生产力的工具体现!
如果你对PDM格式感兴趣:请参考本站PDM标签含义解析:https://cloud.tencent.com/developer/article/2332509
目标PDM文件示例
代码语言:javascript复制<o:Column Id="o77">
<a:ObjectID>64879523-C582-471C-BC11-91177B7D418B</a:ObjectID>
<a:Name>事件类型</a:Name>
<a:Code>EVEN_TYPE</a:Code>
<a:CreationDate>1690874976</a:CreationDate>
<a:Creator>chengfansu</a:Creator>
<a:ModificationDate>1690874976</a:ModificationDate>
<a:Modifier>chengfansu</a:Modifier>
<a:Comment>事件类型</a:Comment>
<a:DefaultValue>NULL</a:DefaultValue>
<a:DataType>int(2)</a:DataType>
<a:Length>2</a:Length>
</o:Column>
<o:Column Id="o78">
<a:ObjectID>B7C5934C-8EF1-4668-92DA-1E94832587B1</a:ObjectID>
<a:Name>推送状态</a:Name>
<a:Code>PUSH_STATE</a:Code>
<a:CreationDate>1690874976</a:CreationDate>
<a:Creator>chengfansu</a:Creator>
<a:ModificationDate>1690874976</a:ModificationDate>
<a:Modifier>chengfansu</a:Modifier>
<a:Comment>推送状态</a:Comment>
<a:DefaultValue>NULL</a:DefaultValue>
<a:DataType>int(2)</a:DataType>
<a:Length>2</a:Length>
</o:Column>
Java代码 – PDM文本替换示例
PDM替换标签代码如下:替换PDM文件中: <o:Column>标签内部的<a:Name>使用<a:Comment>的文本信息
代码语言:javascript复制import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
/**
* 替换PDM文件中: <o:Column>标签内部的<a:Name>使用<a:Comment>的文本信息
* power by chatGPT
*/
public class TagReplacement {
public static void main(String[] args) {
String inputFile = "/XXXX/我是PDM.pdm"; // 输入文件路径
String outputFile = "/XXXX/我是PDM-生成.pdm"; // 输出文件路径
try {
// 读取输入文件
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(inputFile));
// 处理 <o:Column> 标签
NodeList columnList = doc.getElementsByTagName("o:Column");
for (int i = 0; i < columnList.getLength(); i ) {
Element columnElement = (Element) columnList.item(i);
NodeList nameList = columnElement.getElementsByTagName("a:Name");
NodeList commentList = columnElement.getElementsByTagName("a:Comment");
// 检查是否存在 <a:Name> 和 <a:Comment> 标签
if (nameList.getLength() > 0 && commentList.getLength() > 0) {
Element nameElement = (Element) nameList.item(0);
Element commentElement = (Element) commentList.item(0);
// 获取 <a:Comment> 标签的文本内容
String commentText = commentElement.getTextContent();
// 将 <a:Name> 标签的文本内容替换为 <a:Comment> 的文本内容
nameElement.setTextContent(commentText);
}
}
// 将修改后的文档保存为输出文件
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(outputFile));
transformer.transform(source, result);
System.out.println("文本处理完成。");
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:处理完毕的文件头文件是
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8" standalone="no"?><?PowerDesigner AppLocale="UTF16" ID="{805C1741-6027-4C9E-BFD8-D2CB64A1BB0E}" Label="" LastModificationDate="1691505711" Name="数字化设计v0711" Objects="1416" Symbols="84" Target="MySQL 5.0" Type="{CDE44E21-9669-11D1-9914-006097355D9B}" signature="PDM_DATA_MODEL_XML" version="16.5.0.3982"?><!-- do not edit this file --><Model xmlns:a="attribute" xmlns:c="collection" xmlns:o="object">
要改成
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?PowerDesigner AppLocale="UTF16" ID="{805C1741-6027-4C9E-BFD8-D2CB64A1BB0E}" Label="" LastModificationDate="1691505711" Name="数字化设计v0711" Objects="1416" Symbols="84" Target="MySQL 5.0" Type="{CDE44E21-9669-11D1-9914-006097355D9B}" signature="PDM_DATA_MODEL_XML" version="16.5.0.3982"?><!-- do not edit this file --><Model xmlns:a="attribute" xmlns:c="collection" xmlns:o="object">
就是开启新的一行,这样PowerDesigner文件就可读了