Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Moxy Xpath :: Atributos de "Levantamiento"

Tengo un XML que representa un DOM jerárquico donde cada elemento es un <element ...> con cero o más hijos cada uno. Cada <element> tiene una tonelada de atributos y no quiero ensuciar la clase Element con todos esos atributos, dado que también tiene un montón de sus propios métodos.

El primer borrador de la clase Element fue el siguiente. Esto funciona perfectamente:

 class Element { @XmlAttribute private String name; @XmlAttribute private String bounds; // A whole bunch of other attributes @XmlElement(name = "element") List<Element> children; // A whole bunch of other methods }

Traté de mejorar esto por:

 class Element { @XmlPath(".") private Attributes attributes; @XmlElement(name = "element") List<Element> children; // A whole bunch of other methods } class Attributes { @XmlAttribute private String name; @XmlAttribute private String bounds; // A whole bunch of other attributes }

Esto parece funcionar bien, sin embargo, en realidad desordena los datos tras una inspección más cercana. Si ingreso el siguiente XML:

 <element name="Hello" bounds="[0,0][1,1]"> <element name="World" bounds="[1,1][2,2]"> <element name="Testing" bounds="[2,2][3,3]"> <element name="One two three" bounds="[3,3][4,4]" /> </element> </element> </element>

El objeto no ordenado tiene la siguiente estructura/propiedades:

 + [Element] - name = "World" - bounds = "[1,1][2,2]" + children[0] - name = "Testing" - bounds = "[2,2][3,3]" + children[0] - name = "One two three" - bounds = "[3,3][4,4]" + children[0] - name = "One two three" - bounds = "[3,3][4,4]" - children = null

Supuse que XPath(".") "levantaría" los atributos de la clase Attributes a la clase Element principal. Pero, de hecho, está elevando esos atributos dos niveles hacia arriba.

Cuando construyo la jerarquía de Element manualmente, luego trato de organizarla, el XML resultante está bien. Es solo el desarmado lo que está produciendo el objeto incorrecto.

¿Estoy usando XPath incorrectamente aquí? Tengo una solución funcional al incluir todos los atributos directamente dentro de la clase Element , pero solo quería agrupar esos atributos en una clase separada y clasificarlos/desclasificarlos en la clase Element del contenedor.

¡Gracias! Asim

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¿Qué tal usar @XmlAnyAttribute con Map<String,Object>? Eso debería poner todos los atributos que no se tienen en cuenta en el mapa.

 class Element { @XmlAnyAttribute private Map<String,String> attributes; @XmlElement(name = "element") List<Element> children; }

Utilizándolo como element.attributes.get("name)

Alternativamente, podría extender una clase de Attributes común. Estos se verían en el XML como si estuvieran en la clase Element.

 public class Attributes { @XmlAttribute private String name; @XmlAttribute private String bounds; }

y

 class Element extends Attributes { @XmlElement(name = "element") List<Element> children; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!