简体   繁体   English

我们如何跟踪XML属性

[英]How we can tracking attribute of XML

I have 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"/>

I want one element ServicePointID. 我想要一个元素ServicePointID。

How we can tracking attribute event for the SERVICEPOINTFORACCOUNTROW tag using case XMLStreamConstants.ATTRIBUTE 我们如何使用案例XMLStreamConstants.ATTRIBUTE来跟踪SERVICEPOINTFORACCOUNTROW标记的属性事件

Here is the code 这是代码

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"));
        }
    }
}

Output 输出量

987654

Since you mention of using XMLStreamConstants , i am assuming you plan to use XMLStreaming interfaces. 由于您提到使用XMLStreamConstants,因此我假设您打算使用XMLStreaming接口。 One way of doing that is as follows 一种方法如下

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);
            }

Note that the XML thing is not getting reported in a stream/event based interface and hence i had to catch it using START ELEMENT and manipulate that. 请注意,在基于流/事件的接口中未报告XML内容,因此我不得不使用START ELEMENT进行捕获并对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM