java 获取列号,如何在JAVA中使用行号和列号获取XML中的元素

Hi I am using JAXB and Marshal/Unmarshal Schema Validation. I have a XML file and XSD schema for validation. Code are like this:

Validation.java

try{

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Schema schema = sf.newSchema(new File("./src/main/resources/validation.xsd"));

JAXBContext jc = JAXBContext.newInstance(AddCustomer.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();

unmarshaller.setSchema(schema);

unmarshaller.setEventHandler(new MyValidationEventHandler());

AddCustomer addCustomer_Validation= (AddCustomer) unmarshaller.unmarshal(new File("./src/main/resources/AddCustomer.xml"));

logger.info("AddCustomer passed validation.");

} catch(UnmarshalException ex) {

logger.info("linked ex: " + ex.getLinkedException().toString());

String str = ex.getLinkedException().toString();

int lineNumberIndex = str.indexOf("lineNumber:");

int lineNumber = Integer.parseInt(str.substring(lineNumberIndex+12,lineNumberIndex+13));

logger.info("lineNumber:" +lineNumber);

int columnIndex = str.indexOf("columnNumber:");

int columNumber = Integer.parseInt(str.substring(columnIndex+14,columnIndex+16));

logger.info("columnNumber: " + columNumber);

}

Some of the XML file is like this:

32 4 5

2732

From the code above, I could get validate the XML is valid against the XSD. The logger file is something like this:

INFO [main] Validation- linked ex: org.xml.sax.SAXParseException; systemId: file:/C:.../src/main/resources/AddCustomer.xml; lineNumber: 5; columnNumber: 41; cvc-pattern-valid: Value '32 4 5' is not facet-valid with respect to pattern '[ !-~]*' for type 'an..35'.

INFO [main] Validation- lineNumber: 5

INFO [main] Validation- columnNumber: 41

The validation is correct and in the xml file, the value of PortalID is not valid against the XSD schema, and the log file can tell me the location is "lineNumber 5 and ColumNumber 41" which is exactly the place for PortalID.

But what I want is to use this lineNumber 5 and ColumNumber 41 to printout that the element PortalID in the XML is not valid. Is there any way to do that?

Thank you so much!

解决方案

To get the name of the last element parsed you could put a custom ContentHandler that records the last element between the SAXParser and the JAXB handler.

To do that you must use a SAXSource and create a custom XMLFilter implementation extending XMLFilterImpl

pass a SAXSource to the Unmarshaller instead than a File and in the SAXSource configure an XMLReader. This is explained in Unmarshalling from a javax.xml.transform.sax.SAXSource using a client specified validating SAX2.0 parser

Do not configure the SAXSource with the "real" XMLReader implementation as in Javadoc linked code but use the custom XMLFilter that wraps the "real" XMLReader.

XMLReader xmlReader = saxParser.getXMLReader();

RecordingXMLFilter xmlFilter = new RecordingXMLFilter(xmlReader);

SAXSource source =

new SAXSource(xmlFilter, new InputSource(new FileInputStream(new File("./src/main/resources/AddCustomer.xml")));

The custom filter RecordingXMLFilter could be:

private static final class RecordingXMLFilter extends XMLFilterImpl

{

String lastElement;

private RecordingXMLFilter(XMLReader parent)

{

super(parent);

}

@Override

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException

{

lastElement = qName;

super.startElement(uri, localName, qName, attributes);

}

}

In exception handling code use xmlFilter.lastElement to get the QName of the last element parsed.

BTW To get line numbers do in this way, messages could be different in different locales or change between versions.

try

{

..........

}

catch(UnmarshalException ex) {

Throwable linked = ex.getLinkedException();

if (linked instanceof SAXParseException)

{

SAXParseException t = (SAXParseException) linked;

int lineNumber = t.getLineNumber();

int columNumber = t.getColumnNumber();

logger.info("lineNumber:" +lineNumber);

logger.info("columnNumber: " + columNumber);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值