[英]How we can tracking attribute of XML
我有xml
<schemans2:ServicepointForAccountRow AccountID="123456" ServicePointID="987654"
LongDescription="TE Fix Network RES SINGLE PHS/TEP 13 MR/FN TEP Rt 0010/3220 W INA RD, 13203, TUCSON, AZ, 85741-2169, TEP"
UsageInfo="Add"/>
我想要一个元素ServicePointID。
我们如何使用案例XMLStreamConstants.ATTRIBUTE来跟踪SERVICEPOINTFORACCOUNTROW标记的属性事件
这是代码
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.jsoup.parser.Parser;
public class Test {
public static void main(String[] args) {
String xml = "<schemans2:ServicepointForAccountRow AccountID=\"123456\" ServicePointID=\"987654\" LongDescription=\"TE Fix Network RES SINGLE PHS/TEP 13 MR/FN TEP Rt 0010/3220 W INA RD, 13203, TUCSON, AZ, 85741-2169, TEP\" UsageInfo=\"Add\" />";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
List<Node> nodes = doc.childNodes();
for(Node n : nodes) {
System.out.println(n.attr("ServicePointID"));
}
}
}
输出量
987654
由于您提到使用XMLStreamConstants,因此我假设您打算使用XMLStreaming接口。 一种方法如下
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader parser = factory.createXMLStreamReader(new StringReader(xml));
while (parser.hasNext()){
int event = parser.next();
if (event == XMLStreamConstants.START_ELEMENT){
if (parser.getLocalName().equals("schema")){
String servicePointID = parser.getAttributeValue(null, "ServicePointID");
if (servicePointID != null)
System.out.println(servicePointID);
}
请注意,在基于流/事件的接口中未报告XML内容,因此我不得不使用START ELEMENT进行捕获并对其进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.