大家好,又见面了,我是你们的朋友全栈君。
xml示例:
<ResponseContent>
<Parameter>
<Document>
<RecordSet id="1">
<Master name="purchase">
<Record>
<Field name="orderNumber" value="1P05-151200172"/>
<Field name="status" value="Y"/>
<Field name="plant" value="MFL01-1"/>
</Record>
</Master>
</RecordSet>
</Document>
</Parameter>
</ResponseContent>
java解析代码获取Field name=”status” 的value值:
package com.customcode.code;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class ERPGETPARAM {
public static String statusresult(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document document = db.parse(new ByteArrayInputStream(xml.getBytes()));
NodeList node = document.getElementsByTagName("Field");
//for(int i=0;i<node.getLength();i++){
Element element = (Element)node.item(1);
// 获取属性学号
// String name = element.getAttribute("name");
// System.out.println("name:" + name);
String value=null;
try{
value = element.getAttribute("value");
}catch(NullPointerException e){
e.printStackTrace();
}
//System.out.println("value:" + value);
return value;
//获取值
// content = element.getElementsByTagName("value").item(0).getFirstChild().getNodeValue();
// System.out.println("value:" + content);
//}
}
}
main方法运行结果:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137716.html原文链接:https://javaforall.cn