The title is not suitable for programming, especially XML. I'm sorry.

Using Amazon Marketplace Web Service (MWS),
I would like to extract the information such as book name, author name, size, etc. by analyzing the returned XML when I enter ISBN.
Although we succeeded in extracting some information such as book titles from XML,
Author name and size cannot be extracted.

Items that can be extracted, such as product names, have one type of tag pattern.
(Product Name →<ns2: title> ;, Publisher<ns2: Publisher>)

On the other hand, for items that cannot be extracted, there are multiple types of tag patterns, and I do not know how to specify them.
(Author →<ns2: Creator Role ="Author"><ns2: Creator Role ="Translation"><ns2: Creator Role ="Unknown">etc.,
Size →<ns2: Height Units ="inches"><ns2: Height Units ="pixels">etc.)
For each item, it is the same string except in"" ;.

This API can send 5 ISBNs at the same time to get information.
So if all 5 responses contain the tag<ns2: Creator Role ="Translation"> ;, there is no problem,
If even one is not included, a NullPointerException will occur.
It ’s unclear what pattern the tag has, so
If possible, avoid the method of specifying all patterns by performing exception handling.
Is it possible to partially match common character strings?
Also, if there are multiple authors and translations, I would like to extract all of them.

Can you help me?
If there is any missing information, we will supplement it so please point it out.

[Execution result]
The following JAVA code does not generate an error, and the product name, ASIN, classification, and publisher are returned as shown below.

NATURE'S ROBOTS-It's a grand history of protein research, 4860434730, book (softcover), NTS

One ISBN is specified.
I don't know how to write the author/size, and I tried various things, but NullPointerException is returned.
ISBN is delivered by the main method. I'm wondering if it's unnecessary, but if necessary, I'll list the main method.
There is no exception handling, but I will mention it later.

public static void ExportXml () throws ParserConfigurationException, SAXException, IOException {// analyze Xml node
        String responseXml = GetMatchingProductForIdSample.responseXml;
        InputSource inputSource = new InputSource (new StringReader (responseXml));// Convert String type to InputSource type
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
        DocumentBuilder builder = factory.newDocumentBuilder ();
        Document doc = builder.parse (inputSource);
        NodeList nodesTitle = doc.getElementsByTagName ("ns2: Title");// Product name
        NodeList nodesAsin = doc.getElementsByTagName ("ASIN");// ASIN
        NodeList nodesCreator = doc.getElementsByTagName ("ns2: Creator Role = \". * \ "");// I don't know how to write
        NodeList nodesBinding = doc.getElementsByTagName ("ns2: Binding");// Classification
        NodeList nodesPublisher = doc.getElementsByTagName ("ns2: Publisher");// Publisher
        NodeList nodesHeight = doc.getElementsByTagName ("ns2: Height Units = \" inches \ "");// I don't know the size and description method
        for (int i = 0;i&lt;nodesTitle.getLength ();i ++)
            System.out.println (nodesTitle.item (i) .getTextContent () + "," + nodesAsin.item (i) .getTextContent () + "," + nodesBinding.item (i) .getTextContent () + "," + nodesPublisher.item (i) .getTextContent ());
<GetMatchingProductForIdResponse xmlns = "http://mws.amazonservices.com/schema/Products/2011-10-01"><GetMatchingProductForIdResult Id = "9784860434731" IdType = "ISBN" status = "Success"><Products><Product><Identifiers><MarketplaceASIN><MarketplaceId>A1VC38T7YXB528</MarketplaceId><ASIN>4860434730>&SIN;&SIN;&SIN</Identifiers><AttributeSets><ns2: ItemAttributes xml: lang = "en-US" xmlns: ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01/default. xsd "><ns2: Binding>book (softcover)</ns2: Binding><ns2: Creator Role =" Author ">Charles Tanford</ns2: Creator><ns2: Creator Role =" Written by ">Jacqueline Reynolds</ns2: Creator><ns2: Creator Role =" Translation ">Takao Hamakubo</ns2: Creator><ns2: Creator Role =" Translation ">5 other< ;/ns2: Creator><ns2: ItemDimensions><ns2: Height Units = "inches">0.6692913379</ns2: Height>&l t;ns2: Length Units = "inches">8.267716527</ns2: Length><ns2: Width Units = "inches">5.8267716476</ns2: Width></ns2: ItemDimensions><ns2 : IsAdultProduct>false</ns2: IsAdultProduct><ns2: Label>NTS</ns2: Label><ns2: Languages ​​><ns2: Language><ns2: Name>japanese</ns2 : Name><ns2: Type>Published</ns2: Type></ns2: Language></ns2: Languages ​​><ns2: ListPrice><ns2: Amount>3024.00</ns2: Amount><ns2: CurrencyCode>JPY</ns2: CurrencyCode></ns2: ListPrice><ns2: Manufacturer>NTS</ns2: Manufacturer><ns2: NumberOfPages>342</ns2Number ;<ns2: PackageDimensions><ns2: Height Units = "inches">0.8661417314</ns2: Height><ns2: Length Units = "inches">8.4251968418</ns2: Length><ns2 : Width Units = "inches">5.9055118050</ns2: Width><ns2: Weight Units = "pounds">1.0582188576</ns2: Weight></ns2: Pa ckageDimensions><ns2: ProductGroup>Book</ns2: ProductGroup><ns2: ProductTypeName>ABIS_BOOK</ns2: ProductTypeName><ns2: PublicationDate>2018-03-30</ns2: PublicationDate>>ns2: Publisher>;NTS</ns2: Publisher><ns2: SmallImage><ns2: URL>http: //ecx.images-amazon.com/images/I/61b3ceNG8jL._SL75_.jpg</ns2: URL><ns2: Height Units = "pixels">75</ns2: Height><ns2: Width Units = "pixels">53</ns2: Width></ns2: SmallImage><ns2 : Studio>NTS</ns2: Studio><ns2: Title>NATURE'S ROBOTS--the grand history of protein research</ns2: Title></ns2: ItemAttributes></AttributeSets><Relationships></Relationships><SalesRankings><SalesRank><ProductCategoryId>book_display_on_website</ProductCategoryId><Rank>361226</Rank><&Rank></ProductCategoryI d><Rank>74</Rank></SalesRank><SalesRank><ProductCategoryId>492162</ProductCategoryId><Rank>4523</Rank></SalesRank><<&king/Product></Products></GetMatchingProductForIdResult><ResponseMetadata><RequestId>7efe54c2-8a52-4b16-9cef-466971ff6aa6</RequestId></ResponseMetadata></GetMgtingProductForpre>
  • Answer # 1

    Tags (elements) and attributes seem to be messed up.
    For example, for<ns2: Creator Role ="Author">:
    (To be exact, the description of the namespace is also necessary, but this time it is unnecessary, so it is omitted)

    Tag name (element name):ns2: Creator

    Attribute name:Role

    Attribute value:Author

    If you look at the source code,doc.getElementsByTagName ("ns2: Creator Role = \". * \"")is written, but you can specify it as an argument. Only the tag name (element name). So you need to do something likedoc.getElementsByTagName ("ns2: Creator").


    How to getRole, you need to useElement # getAttribute ()method. Also, the text enclosed between<ns2: Creator ...>to</ns2: Creator>isElement # getTextContent ()method.

    Finally, you can get it as follows (only Creator is supported).

    // omitted
    NodeList nodesCreator = doc.getElementsByTagName ("ns2: Creator");
    // omitted
    for (int i = 0;i<nodesTitle.getLength ();i ++) {
        // omitted
        for (int j = 0;j<nodesCreator.getLength ();j ++) {
            Element creator = (Element) nodesCreator.item (j);
            String role = creator.getAttribute ("Role");
            String creatorName = creator.getTextContent ();
            System.out.println (role + ":" + creatorName);