通过XML文件创建对象数组-Javascript

时间:2018-08-15 04:35:28

标签: javascript xml

我正在尝试在JavaScript中创建对象数组,并从XML文件填充每个对象。 XML文件已被加载到以前的函数中。但是,当我尝试推出新产品时,控制台中出现错误,提示x [i]未定义。

我的代码是:

function CreateProductObject() {
  function productObject(name, brand, price, info, material, photo) {
    this.name = name;
    this.brand = brand;
    this.price = price;
    this.info = material;
    this.photo = photo;
  }
  var productArray = [];

  var numOfProducts = CountProducts(productType);
  console.log(productType + " needs to be Displayed: " + numOfProducts);

  x = xmlDoc.getElementsByTagName(productType);

  for (i = 0; i <= numOfProducts; i++) {
    productArray.push(
      new productObject(
        x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue,
        x[i].getElementsByTagName("BRAND")[0].childNodes[0].nodeValue,
        x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue,
        x[i].getElementsByTagName("INFO")[0].childNodes[0].nodeValue,
        x[i].getElementsByTagName("MATERIAL")[0].childNodes[0].nodeValue,
        x[i].getElementsByTagName("PHOTO")[0].childNodes[0].nodeValue,
      ),
    );

    console.log(productArray[i].name);
  }
}

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:-2)

您尚未声明apply plugin: 'com.android.application' apply plugin: 'io.fabric' android { configurations { all*.exclude module: 'okhttp' all*.exclude module: 'okio' } compileSdkVersion 27 buildToolsVersion '27.0.2' defaultConfig { applicationId "com.abc.def" vectorDrawables.useSupportLibrary = true minSdkVersion 17 targetSdkVersion 27 versionCode 15 versionName "1.2.4" multiDexEnabled = true } buildTypes { release { lintOptions { disable 'MissingTranslation' checkReleaseBuilds false } minifyEnabled false } } aaptOptions { cruncherEnabled = false } dexOptions { javaMaxHeapSize "2g" } } repositories { maven { url 'https://maven.fabric.io/public' } maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:design:27.0.2' compile 'com.android.support:appcompat-v7:27.0.2' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:percent:27.0.2' compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.jakewharton:butterknife:8.5.1' compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.android.support:recyclerview-v7:27.0.2' compile 'com.android.support:cardview-v7:27.0.2' provided 'org.projectlombok:lombok:1.16.16' compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.8' annotationProcessor "org.projectlombok:lombok:1.16.16" compile 'com.google.android.gms:play-services-maps:11.0.1' testCompile 'junit:junit:4.12' annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.0' compile 'com.theartofdev.edmodo:android-image-cropper:2.4.+' compile 'com.mikhaellopez:circularimageview:3.0.2' compile 'com.wdullaer:materialdatetimepicker:3.2.2' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.github.jd-alexander:library:1.1.0' compile 'com.google.android.gms:play-services-location:11.0.1' compile 'com.google.android.gms:play-services-maps:11.0.1' compile 'com.yarolegovich:discrete-scrollview:1.2.0' compile 'com.miguelcatalan:materialsearchview:1.4.0' compile 'com.wang.avi:library:2.1.3' compile 'com.google.firebase:firebase-core:11.0.1' compile 'com.google.firebase:firebase-messaging:11.0.1' compile 'com.android.support:multidex:1.0.0' compile 'com.github.ksoichiro:android-observablescrollview:1.5.0' compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' compile 'com.hedgehog.ratingbar:app:1.1.2' compile 'com.eftimoff:android-pathview:1.0.8@aar' compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') { transitive = true; } compile 'uk.co.chrisjenx:calligraphy:2.3.0' apply plugin: 'com.google.gms.google-services' compile 'com.stone.vega.library:VegaLayoutManager:1.0.1' compile project(':library') compile 'me.henrytao:smooth-app-bar-layout:24.1.0.0' compile project(':PayTabs_SDK') /*----------------payfort sdk------------*/ implementation 'com.victor:lib:1.0.1' implementation 'com.shamanland:fonticon:0.1.8' implementation('com.nispok:snackbar:2.11.+') { // exclusion is not necessary, but generally a good idea. exclude group: 'com.google.android', module: 'support-v7' } implementation 'com.google.guava:guava:18.0' implementation 'org.bouncycastle:bcprov-jdk16:1.46' implementation 'commons-codec:commons-codec:1.10' implementation project(':FORTSDKv1.4.4') } ,因此未定义。