我有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元素。
我们如何使用case XMLStreamConstants.ATTRIBUTE跟踪SERVICEPOINTFORACCOUNTROW标记的属性事件
答案 0 :(得分:1)
以下是代码
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
答案 1 :(得分:0)
由于您提到使用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捕获它并操纵它。