def read(cls, path: Path) -> "GeneratorConfig": ctx = XmlContext( element_name_generator=text.pascal_case, attribute_name_generator=text.camel_case, ) config = ParserConfig(fail_on_unknown_properties=False) parser = XmlParser(context=ctx, config=config) return parser.from_path(path, cls)
def serialize_xml_from_file(xml_file_path: pathlib.Path, serialize_clazz: Optional[Type[T]]): """ Method to serialize XML data from a file. :param xml_file_path: A pathlib.path path object that leads to the targeted XML file. :param serialize_clazz: A class Object. :return: the serialized object. """ parser = XmlParser(context=XmlContext()) return parser.from_path(xml_file_path, serialize_clazz)
def fetch_test_cases() -> Iterator[TestCase]: suite = XmlParser().from_path(w3c.joinpath("suite.xml"), TestSuite) parser = XmlParser() for test_set_ref in suite.test_set_ref: test_set_ref_path = w3c.joinpath(test_set_ref.href) path = test_set_ref_path.parent test_set = parser.from_path(test_set_ref_path, TestSet) for test_group in reversed(test_set.test_group): for test_case in make_test_cases(path, test_group): yield test_case
def assert_bindings( schema: str, instance: str, class_name: str, version: str, mode: str, save_output: bool, output_format: str, structure_style: str, ): __tracebackhide__ = True if mode == "xml": instance_path = Path(instance) pck_arr = list(map(text.snake_case, instance_path.parts)) package = f"output.xml_models.{'.'.join(pck_arr)}" instance_path = w3c.joinpath(instance_path) source = str(instance_path) else: schema_path = Path(schema) pck_arr = list(map(text.snake_case, schema_path.parts)) package = f"output.models.{'.'.join(pck_arr)}" schema_path = w3c.joinpath(schema) source = str(schema_path) clazz = generate_models(source, package, class_name, output_format, structure_style) if mode == "build": return if isinstance(clazz, Exception): raise clazz try: instance_path = w3c.joinpath(instance) schema_path = w3c.joinpath(schema) context = XmlContext(class_type=output_format) parser = XmlParser(context=context) obj = parser.from_path(instance_path, clazz) except Exception as e: raise e save_path = None if save_output: save_path = output.joinpath(instance) save_path.parent.mkdir(parents=True, exist_ok=True) if mode == "json": assert_json_bindings(context, obj, save_path) else: assert_xml_bindings( context, obj, parser.ns_map, schema_path, instance_path, save_path, version )
def deserialize(resource: Union[str, Path, bytes], target_class: Optional[Type[T]] = None) -> Optional[T]: parser = XmlParser(context=XmlContext()) obj = None if os.path.isfile(resource): resource = Path(resource) if isinstance(resource, str): obj = parser.from_string(resource, target_class) if isinstance(resource, Path): obj = parser.from_path(resource, target_class) if isinstance(resource, bytes): obj = parser.from_bytes(resource, target_class) if obj and hasattr(obj, 'complemento') and obj.complemento: complementos = __deserialize_complementos(obj) setattr(obj.complemento, 'any_element', complementos) return obj
def test_xml_documents(): filepath = fixtures_dir.joinpath("artists") package = "tests.fixtures.artists" runner = CliRunner() result = runner.invoke(cli, [str(filepath), "--package", package]) if result.exception: raise result.exception clazz = load_class(result.output, "Metadata") parser = XmlParser() serializer = XmlSerializer(writer=XmlEventWriter) serializer.config.pretty_print = True serializer.config.xml_declaration = False ns_map = {None: "http://musicbrainz.org/ns/mmd-2.0#"} for i in range(1, 4): ap = filepath.joinpath(f"art00{i}.xml") obj = parser.from_path(ap, clazz) actual = serializer.render(obj, ns_map) assert ap.read_bytes().splitlines() == actual.encode().splitlines()
from pathlib import Path from tests.fixtures.primer import PurchaseOrder from xsdata.formats.dataclass.parsers import XmlParser parser = XmlParser() filepath = Path("tests/fixtures/primer/order.xml") order = parser.from_path(filepath, PurchaseOrder) order.bill_to
import sys from pathlib import Path from xsdata.formats.dataclass.parsers import XmlParser from xsdata.formats.dataclass.serializers import XmlSerializer from xsdata.formats.dataclass.serializers.config import SerializerConfig from reqif.models import ReqIf here = Path(__file__).parent xml_fixture = here.joinpath("sample.xml") parser = XmlParser() config = SerializerConfig(pretty_print=True, encoding="ascii") serializer = XmlSerializer(context=parser.context, config=config) obj = parser.from_path(xml_fixture, ReqIf) ns_map = { None: "http://www.omg.org/spec/ReqIF/20110401/reqif.xsd", "xhtml": "http://www.w3.org/1999/xhtml", } serializer.write(sys.stdout, obj, ns_map=ns_map)
def open_model(self, filename: Path): parser = XmlParser(context=XmlContext()) stm = parser.from_path(filename, Model) self.modelOpened.emit(stm)
def open_template(self, filename: Path): parser = XmlParser(context=XmlContext()) stt = parser.from_path(filename, Template) self.templateOpened.emit(stt)