<ItemInfo>
<ItemStatistics>商品统计</ItemStatistics>
<Items>
<Item>
<ItemName>iPhone</ItemName>
<ItemNum>3</ItemNum>
<ItemValue>1000000</ItemValue>
</Item>
</Items>
<Remark>配送</Remark>
</ItemInfo>
将上面的文本转为xml文件,保存,并获取xml文件指定节点中的内容:
import org.json.JSONArray;
import java.io.FileWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
private String getPdfInfo(String requestInfo) {
JSONArray array = new JSONArray();
try {// 将文件保存到指定目录下
String savePath = "D:/request.xml";
FileWriter fw = new FileWriter(savePath);
fw.write(requestInfo);
fw.flush();
fw.close();
// 获取指定节点的内容
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(savePath));
NodeList pdfInfo = doc.getElementsByTagName("ItemName");
for (int i = 0; i < pdfInfo.getLength(); i++) {
String itemName = pdfInfo.item(i).getTextContent();
System.out.println(itemName);
array.put(itemName);
}
} catch (Exception e) {
e.printStackTrace();
}
return array.toString();
}