如何使用Java在XML中查找未闭合的标签? [英] How to find unclosed tags in XML with Java?

查看:406
本文介绍了如何使用Java在XML中查找未闭合的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些错误的XML文档 - 有时会丢失结束标记 - 我想找到发生这种情况的地方并手动修复它们。



我已经使用XOM解析文档,并在适当的时候轻松地说缺少结束标记,并告诉我元素的名称,但不能很好地指导我在文件中的问题所在。 / p>

我可以编写自己的解析器来帮助完成此操作,但我想知道是否已有解决方案?我不想自动整理,因为我想确保在正确的位置插入结束标签。我只是想知道开始标记的行号。

解决方案

我认为这很简单,可以在没有任何第三方的情况下完成图书馆。 Java有标准类
javax.xml.stream.XMLEventReader ,它会在找到时抛出 XMLException 错过结束标记。然后调用 e.getLocation()。getLineNumber()来获取行号。



有点复杂的样本:

  InputStream is = new FileInputStream(test.xml); 
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(is,utf-8);
Stack< StartElement> stack = new Stack< StartElement>();
while(eventReader.hasNext()){
try {
XMLEvent event = eventReader.nextEvent();
if(event.isStartElement()){
StartElement startElement = event.asStartElement();
System.out.println(processing element:+ startElement.getName()。getLocalPart());
stack.push(startElement);
}
if(event.isEndElement()){
stack.pop();
}
} catch(XMLStreamException e){

System.out.println(行内错误:+ e.getLocation()。getLineNumber());
StartElement se = stack.pop();
System.out.println(非封闭标签:+ se.getName()。getLocalPart()++ se.getLocation()。getLineNumber());

扔e;
}
}


I have some XML documents with errors in - sometimes end tags are missing - and I want to find the places where this happens and fix them (manually).

I've used XOM to parse the documents and it handily says "missing end tag" at the right times, and tells me the name of the element, but doesn't guide me very well to where the problem is in the file.

I could write my own parser that helps to do this, but I wonder if there's already a solution? I don't want automatic tidying, as I want to make sure end tags are inserted in the right place. I just want to know the line number of the start tag.

解决方案

I think it simple and can be done without any 3rd party library. Java has standart class javax.xml.stream.XMLEventReader, and it will throw XMLException when it find missed end tag. Then call e.getLocation().getLineNumber() to get line number.

a bit complecated sample:

    InputStream is = new FileInputStream("test.xml");
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(is, "utf-8");
    Stack<StartElement> stack = new Stack<StartElement>();
    while (eventReader.hasNext()) {
        try {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();
                System.out.println("processing element: " + startElement.getName().getLocalPart());
                stack.push(startElement);
            }
            if(event.isEndElement()){
                stack.pop();
            }
        }catch(XMLStreamException e){

            System.out.println("error in line: " +e.getLocation().getLineNumber());
            StartElement se = stack.pop();
            System.out.println("non-closed tag:" + se.getName().getLocalPart() + " " + se.getLocation().getLineNumber());

            throw e;
        }
    }

这篇关于如何使用Java在XML中查找未闭合的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆