读/写期间的XmlBeans源定位器

时间:2012-04-20 07:22:37

标签: java xml serialization xmlbeans

我有从XSD生成的XmlBeans类。我想跟踪持久对象的行号。这可能吗?我不关心在解析(xml→beans)期间或者在prettyprinting(beans→xml)期间是否存储了这些信息,因为我在应用程序流程中保持它们同步。

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

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

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

2 个答案:

答案 0 :(得分: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("]");
        }

    }

}

input.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)和名为@XmlLocation的引用实现都支持JAXB扩展(下面是使用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;
    }

}

答案 1 :(得分: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();
        }
    }
}