1

我有从 XSD 生成的 XmlBeans 类。我想跟踪持久对象的行号。这可能吗?我不在乎这些信息是在解析期间(xml → beans)还是在漂亮打印(beans → xml)期间存储的,因为我让它们在我的应用程序流中保持同步。

如果可能的话,我想要开始和结束行/列号。

我不在乎我是否必须使用某种非标准黑客来获取定位器数据。

如果有另一个 Java XML 框架可以从 XSD 文件生成类支持定位器数据,那么我愿意切换。

4

2 回答 2

2

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

如果有另一个 Java XML 框架可以从 XSD 文件生成类并支持定位器数据,那么我愿意切换。

JAXB 可以从 XML 模式生成类,下面是几种获取位置信息的方法。有关 JAXB 和 XMLBeans 的比较,请参见:

选项 #1 - StAX 和 Unmarshaller.Listener

演示

package forum10241929;

import java.io.File;
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File("src/forum10241929/input.xml")));

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setListener(new LocationListener(xsr));
        Customer customer = (Customer) unmarshaller.unmarshal(xsr);
    }

    private static class LocationListener extends Unmarshaller.Listener {

        private XMLStreamReader xsr;

        public LocationListener(XMLStreamReader xsr) {
            this.xsr = xsr;
        }

        @Override
        public void afterUnmarshal(Object target, Object parent) {
            log("End", target);
        }

        @Override
        public void beforeUnmarshal(Object target, Object parent) {
            log("Start", target);
        }

        private void log(String event, Object target) {
            System.out.print(event);
            System.out.print(" ");
            System.out.print(target);
            System.out.print(" [");
            Location location = xsr.getLocation();
            System.out.print(location.getLineNumber());
            System.out.print(",");
            System.out.print(location.getColumnNumber());
            System.out.println("]");
        }

    }

}

输入.xml

<?xml version="1.0" encoding="UTF-8"?>
<customer>
    <name>Jane Doe</name>
    <address>
        <street>1 A Street</street>
    </address>
</customer>

输出

Start forum10241929.Customer@144aa0ce [2,11]
Start forum10241929.Address@19e3cd51 [4,14]
End forum10241929.Address@19e3cd51 [6,15]
End forum10241929.Customer@144aa0ce [7,12]

顾客

package forum10241929;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private String name;
    private Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}

地址

package forum10241929;

public class Address {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

选项 #2 - @XmlLocation

EclipseLink JAXB (MOXy) 和调用的参考实现都支持 JAXB 扩展@XmlLocation(下面是使用 MOXy 的示例)。这只会捕获开始位置。

package forum10241929;

import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlLocation;
import org.xml.sax.Locator;

@XmlRootElement
public class Customer {

    private String name;
    private Address address;

    @XmlLocation
    private Locator location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}
于 2012-04-20T10:16:49.910 回答
1

只需使用XmlOptions#setLoadLineNumbers(),例如:

MyDocument.Factory.parse(xmlFile, new XmlOptions().setLoadLineNubmers());

然后要从 xml 存储中检索行号,请找到最近的 XmlLineNumber 书签。

import org.apache.xmlbeans.*;

public class linenumber
{
    public static void main(String[] args) throws XmlException
    {
        XmlOptions options = new XmlOptions().setLoadLineNumbers();
        XmlObject xobj = XmlObject.Factory.parse("<a>\n<b>test</b>\n<c>test</c>\n</a>", options);

        // let's get the line number for the '<c>' xml object
        XmlObject cobj = xobj.selectPath(".//c")[0];
        System.out.println(cobj.xmlText());

        XmlCursor c = null;
        try
        {
            c = cobj.newCursor();

            // search for XmlLineNumber bookmark
            XmlLineNumber ln =
                (XmlLineNumber) c.getBookmark( XmlLineNumber.class );

            if (ln == null)
                ln = (XmlLineNumber) c.toPrevBookmark( XmlLineNumber.class );

            if (ln != null)
            {
                int line = ln.getLine();
                int column = ln.getColumn();
                int offset = ln.getOffset();

                System.out.println("line=" + line + ", col=" + column + ", offset=" + offset);
            }
        }
        finally
        {
            if (c != null) c.dispose();
        }
    }
}
于 2012-04-20T23:21:42.483 回答