3 回答
TA贡献1798条经验 获得超3个赞
Java 方法。我使用了 JDK 8,它提供了内置的 SAX 解析器。
abc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<sweep runs="1">
<parameter name="Date" type="number" number_type="int" start="2" end="4" step="1">
<parameter name="table" type="list" value_type="java.lang.String" values="" />
</parameter>
</sweep>
Example.java :
import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Example {
public static void main(String[] args)
throws ParserConfigurationException, SAXException, TransformerException {
List<String> values = Arrays.asList("010WE", "021WG", "022X");
// Read xml
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
File file = new File("D:/abc.xml");
Document document = documentBuilder.parse(file);
NodeList list = document.getElementsByTagName("parameter");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
NamedNodeMap map = node.getAttributes();
if (map.getNamedItem("name").getNodeValue().equalsIgnoreCase("table")) {
map.getNamedItem("values").setNodeValue(
values.toString().replace(",", "").replaceAll("\\s+", " ").replace("[", "").replace("]", ""));
}
}
// modify xml by overwriting it.
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
document.setXmlStandalone(true);
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(file);
transformer.transform(source,result);
}
}
修改后的 abc.xml :
<?xml version="1.0" encoding="UTF-8"?>
<sweep runs="1">
<parameter end="4" name="Date" number_type="int" start="2" step="1" type="number">
<parameter name="table" type="list" value_type="java.lang.String" values="010WE 021WG 022X"/>
</parameter>
</sweep>
TA贡献1876条经验 获得超7个赞
以下
XML_TEMPLATE = '''
<?xml version="1.0" encoding="UTF-8"?>
<sweep runs="1">
<parameter name="Date" type="number" number_type="int" start="2" end="4" step="1">
<parameter name="table" type="list" value_type="java.lang.String" values="{}" />
</parameter>
</sweep> '''
values = ['a','b','c']
xml = XML_TEMPLATE.format(' '.join(values))
print(xml)
TA贡献1853条经验 获得超6个赞
解决方案:
此python脚本将在项目的根目录中生成一个 example.xml:
import xml.etree.ElementTree as ET
def prettify(element, indent=' '):
queue = [(0, element)] # (level, element)
while queue:
level, element = queue.pop(0)
children = [(level + 1, child) for child in list(element)]
if children:
element.text = '\n' + indent * (level+1) # for child open
if queue:
element.tail = '\n' + indent * queue[0][0] # for sibling open
else:
element.tail = '\n' + indent * (level-1) # for parent close
queue[0:0] = children # prepend so children come before siblings
def create_xml(input_list):
xml_doc = ET.Element('sweep', runs='1')
parameter = ET.SubElement(xml_doc, 'parameter', name='Date', type='number', number_type='int', start='2', end='4', step='1')
ET.SubElement(parameter, 'parameter', name='table', type='list', value_type='java.lang.String', values=' '.join(input_list))
prettify(xml_doc)
tree = ET.ElementTree(xml_doc)
tree.write('example.xml', encoding='UTF-8', xml_declaration=True)
def main():
some_list = ['010WE', '021WG', '022X']
create_xml(some_list)
if __name__ == '__main__':
main()
基于帖子的答案:Pretty printing XML in Python
添加回答
举报