-1
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="XXXXXXX">
<cuobject type-id="EmailSubscription" object-id="jjjil@gmail.com">
    <object-attribute attribute-id="exported">true</object-attribute>
    <object-attribute attribute-id="firstName">jjj</object-attribute>
    <object-attribute attribute-id="lastName">jjj</object-attribute>
    <object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>

<cuobject type-id="EmailSubscription" object-id="gggj@gmail.com">
    <object-attribute attribute-id="exported">true</object-attribute>
    <object-attribute attribute-id="firstName">ghh</object-attribute>
    <object-attribute attribute-id="lastName">fhh</object-attribute>
    <object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>

<cuobject type-id="EmailSubscription" object-id="mmm@gmail.com">
    <object-attribute attribute-id="exported">true</object-attribute>
    <object-attribute attribute-id="firstName">mmm</object-attribute>
    <object-attribute attribute-id="lastName">mmm</object-attribute>
    <object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>
</objects>

How to read this xml file in java (need to be done by using exported,firstName,lastName,...)? how do we do that? I am doing it like

NodeList nList = doc.getElementsByTagName("cuobject");

for (int temp = 0; temp < nList.getLength(); temp++) 
{               

    Node nNode = nList.item(temp);       

    System.out.println("\nCurrent Element :" + nNode.getNodeName());         

    if (nNode.getNodeType() == Node.ELEMENT_NODE) 
    {        

        Element eElement = (Element) nNode;

        String email = eElement.getAttribute("object-id");

        String firstName =  eElement.

        getElementsByTagName("object-attribute").item(1).getTextContent();

        String lastName = eElement.

        getElementsByTagName("object-attribute").item(2).getTextContent();

        String subscribed = eElement.

        getElementsByTagName("object-attribute").item(3).getTextContent();

    }
}
4

2 回答 2

0

您可以使用 XPath 查询和 Java 支持它们来查询 Xml 文档。

您可以参考How to read XML using XPath in Java了解更多信息

于 2013-05-22T10:41:01.783 回答
0

如果您对 JAXB 没有任何问题,我们开始吧

  1. 将您的 xml 转换为 XSD。

    您可以在线将 xml 转换为 XSD,例如 ( http://www.freeformatter.com/xsd-generator.html )

  2. 将 XSD 转换为 Java 类

    如果您使用的是 eclipse juno,您可以轻松地将其架构转换为 Java 类。在此处查看详细步骤(http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-in-eclipse-ide/

  3. 使用 XML 作为对象(符合 OOPS,非常适合您的客户详细信息 XML)来访问值

    公共类 MyTestClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("C:\\kar\\file1.xml");
        try{
        JAXBContext jaxbContext = JAXBContext.newInstance(Objects.class);
        Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();
    
    
        Objects listcustomers = (Objects) jaxbUnMarshaller.unmarshal(file);
        List<Cuobject> cusmters=listcustomers.getCuobject();
        Iterator<Cuobject> it = cusmters.iterator();
        while(it.hasNext()){
            Cuobject customer = it.next();
            System.out.println("Customer ID: "+ customer.typeId + " Customer email" + customer.objectId);
            List<ObjectAttribute> details =customer.objectAttribute;
            Iterator<ObjectAttribute> it1 = details.iterator();
            System.out.println(" -------Details------------");
            while(it1.hasNext()){
                ObjectAttribute detail = it1.next();
    
                System.out.println(detail.attributeId + " =" + detail.value);
            }
        }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
    

    }

  4. 输出将是这样的

    客户 ID:EmailSubscription 客户 emailjjjil@gmail.com --------详细信息------------ 导出 =true firstName =jjj lastName =jjj subscribed =true

    Customer ID: EmailSubscription Customer emailgggj@gmail.com
     -------Details------------
    exported =true
    firstName =ghh
    lastName =fhh
    subscribed =true
    Customer ID: EmailSubscription Customer emailmmm@gmail.com
     -------Details------------
    exported =true
    firstName =mmm
    lastName =mmm
    subscribed =true
    
于 2013-05-22T11:32:15.280 回答