How to get XPath of all Nodes in XML by using Java or Scala?

时间:2018-02-03 10:48:52

标签: java xml scala xpath

I want to get XPath of all Nodes in XML by using Java or Scala ?

<foo>
    <foo1>Foo Test 1</foo1>
    <foo2>
        <another1>
            <test10>This is a duplicate</test10>
        </another1>
    </foo2>
    <foo2>
        <another1>
            <test1>Foo Test 2</test1>
        </another1>
    </foo2>
    <foo3>Foo Test 3</foo3>
    <foo4>Foo Test 4</foo4>
</foo>

Output :

foo
foo/foo2/
/foo/foo2/another1/

1 个答案:

答案 0 :(得分:0)

我认为您需要的是使用StAX parser。请考虑以下代码:

private final Handler handler = new Handler();
final Runnable showDialogRunnable = new Runnable() {
            @Override
            public void run() {
                builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("You are about to set the Nearby friends radius on " + rangeBarValue + "km") ;
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(true);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(false);
                    }
                });
                alertDialog = builder.create();
                alertDialog.show();
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.alertDialogPositiveButton));
                alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.alertDialogNegativeButton));
            }
        };

它是public class XmlPathIterator implements Iterator<String> { private static XMLInputFactory factory = XMLInputFactory.newFactory(); private final XMLStreamReader xmlReader; private List<String> tags = new ArrayList<>(); // really need just Stack but it is old and Vector-based public XmlPathIterator(XMLStreamReader xmlReader) { this.xmlReader = xmlReader; moveNext(); } public static XmlPathIterator fromInputStream(InputStream is) { try { return new XmlPathIterator(factory.createXMLStreamReader(is)); } catch (XMLStreamException e) { throw new RuntimeException(e); } } public static XmlPathIterator fromReader(Reader reader) { try { return new XmlPathIterator(factory.createXMLStreamReader(reader)); } catch (XMLStreamException e) { throw new RuntimeException(e); } } private void moveNext() { try { while (xmlReader.hasNext()) { int type = xmlReader.next(); switch (type) { case XMLStreamConstants.END_DOCUMENT: tags.clear(); // finish return; case XMLStreamConstants.START_ELEMENT: QName qName = xmlReader.getName(); tags.add(qName.getLocalPart()); return; case XMLStreamConstants.END_ELEMENT: tags.remove(tags.size() - 1); break; // but continue the loop! // also continue the loop on everything else } } } catch (XMLStreamException ex) { throw new RuntimeException(ex); // just pass throw } } @Override public boolean hasNext() { return !tags.isEmpty(); } @Override public String next() { String cur = "/" + String.join("/", tags); moveNext(); return cur; } } 的迭代器,它为每个节点返回XPath。如果您的文件很小并且适合内存,则可以轻松地从中构建String

未处理的事情:

  1. 命名空间(您的示例没有),但您可以在List

  2. 中修改String生成QName的方式
  3. 位置指定程序,以防在同一路径下有多个匹配的标记。如果您只想获取唯一字符串,可以从此迭代器创建case XMLStreamConstants.START_ELEMENT以过滤掉重复项。