簡體   English   中英

我們如何跟蹤XML屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM